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

前端 未结 6 2149
深忆病人
深忆病人 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:14

    Like

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM");
    Date utilDate = formatter.parse(year + "/" + month);
    

    Copied from Create a java.util.Date Object from a Year, Month, Day Forma or maybe like

    DateTime aDate = new DateTime(year, month, 1, 0, 0, 0);
    

    Copied from What's the Right Way to Create a Date in Java?

提交回复
热议问题