Java date formatting?

后端 未结 4 537
深忆病人
深忆病人 2021-01-19 16:40

I want to read a date in the format YYYY-MM-DD.

But if I enter date say for example 2008-1-1, I want to read it as 2008-01-01.

Can anybody help me? Thanks

4条回答
  •  南方客
    南方客 (楼主)
    2021-01-19 17:29

    Or use the much better Joda Time lib.

        DateTime dt = new DateTime();
        System.out.println(dt.toString("yyyy-MM-dd"));
    
        // The ISO standard format for date is 'yyyy-MM-dd'
        DateTimeFormatter formatter = ISODateTimeFormat.date();
        System.out.println(dt.toString(formatter));
        System.out.println(formatter.print(dt));
    

    The Date and Calendar API is awful.

提交回复
热议问题