javaparsingdatetimejodatime

Joda time does not parse months correctly


I have this simple piece o code :

DateTimeFormatter format = DateTimeFormat.forPattern("YYYY MM DD");
DateTime dateTime;

String buffer = "2013 01 01";
dateTime = format.parseDateTime(buffer);
System.out.println(dateTime.getMonthOfYear());

buffer = "2013 02 01";
dateTime = format.parseDateTime(buffer);
System.out.println(dateTime.getMonthOfYear());

and output is: 1 1

but I would expect: 1 2

...where is the problem? I don't need to get month of year, but I have problem with determining dayOfWeek, because dateTime has always January and not other months.


Solution

  • If I understand you need to check your pattern:

    DateTimeFormatter format = DateTimeFormat.forPattern("yyyy MM dd");
    

    where

    y       year
    d       day of month