Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java date formatting?

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 in advance

like image 620
user48094 Avatar asked Nov 27 '22 13:11

user48094


1 Answers

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.

like image 82
Ludwig Wensauer Avatar answered Dec 09 '22 19:12

Ludwig Wensauer