How to generate a Date from just Month and Year in Java?

前端 未结 6 2156
深忆病人
深忆病人 2021-02-12 11:30

I need to generate a new Date object for credit card expiration date, I only have a month and a year, how can I generate a Date based on those two? I need the easiest way possib

6条回答
  •  鱼传尺愫
    2021-02-12 12:19

    You could use java.util.Calendar:

    Calendar calendar = Calendar.getInstance();
    calendar.clear();
    calendar.set(Calendar.MONTH, month);
    calendar.set(Calendar.YEAR, year);
    Date date = calendar.getTime();
    

提交回复
热议问题