Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joda Time: Why does Month value default to Jan regardless of date input?

Tags:

java

jodatime

This is my first time using Joda-Time. Why does the month default to January? It doesn't matter what month values i enter as date including (1-12) or (Jan-Dec). All default to January.

DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy/MM/DD HH:mm:ss");
DateTime issuedTimeStamp = fmt.parseDateTime("2014/04/30 08:23:36");

System.out.println("Issued: " + issuedTimeStamp.toString()); 
//above prints `2014-01-30T08:23:36.000-05:00`

Ive checked my pattern which seems right. Where am i going wrong? Thank you.

like image 770
medokr Avatar asked Sep 16 '25 19:09

medokr


1 Answers

You're using DD in your format string, which means "day of year". So after parsing the month as April, you're then going to the 30th day of the year, which is in January... You want dd, for "day of month".

When in doubt, if a format string doesn't do what you expect:

  • Check every format specifier against the documentation
  • Check that you're escaping everything you should
  • Try formatting a hard-coded value (ideally similar to one that's failing to parse)
like image 132
Jon Skeet Avatar answered Sep 18 '25 10:09

Jon Skeet