Convert a string to a GregorianCalendar

前端 未结 2 1280
南笙
南笙 2020-11-29 11:50

How do a I take an input birthday string such as 02 26 1991 and make it into a Gregorian Calendar?

I tried parsing it first but it keeps giving me an error message s

2条回答
  •  无人及你
    2020-11-29 12:29

    Use SimpleDateFormat to parse the date and then assign it to a Calendar.

    DateFormat df = new SimpleDateFormat("dd MM yyyy");
    Date date = df.parse("02 26 1991");
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    

    The third line could be replaced with:

    Calendar cal = new GregorianCalendar();
    

    but I prefer the first version.

提交回复
热议问题