Reputation: 1485
I'm trying to use the Joda library's DateTimeFormatter to parse a DateTime object from a String. For some reason it is throwing an exception when it reaches the time zone to parse.
Here is my code:
protected static final String DEFAULT_TIMEZONE = "America/Denver";
// Parse the date for the note
DateTimeFormatter dtFormatter = DateTimeFormat.forPattern("MM/dd/yyyy hh:mm a ZZZ");
String dateStr = defaultMonth + "/15/" + defaultYear + " 12:00 am " + DateTimeZone.forID(DEFAULT_TIMEZONE);
try {
DateTime noteDate = dtFormatter.parseDateTime(dateStr);
} catch(Exception e) {
e.printStackTrace();
}
StackTrace:
java.lang.IllegalArgumentException: Invalid format: "11/15/2014 12:00 am America/Denver" is malformed at "America/Denver" at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:683) at com.happyjacksoftware.emr.actions.AddreportnotesController.create(AddreportnotesController.java:56) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597)
I don't actually take in any time as an input, so I was trying the pattern string "MM/dd/yyyy ZZZ"
but this did not work, and I thought maybe the DateTime object needed a time to be instantiated. Adding the time however did not work.
NOTE: I did try this with the pattern string "MM/dd/yyyy hh:mm a Z"
and string 11/15/2014 12:00 am -0700
and it worked, but getting the numeric offset is much less convenient for me, so I was hoping there is a solution to use the Timezone ID.
Upvotes: 1
Views: 1594
Reputation: 14149
I've checked your code on JodaTime 2.1 - 2.3 and it's working properly. I think that you use some old version which can behave in different way. In current versions of Joda parseDateTime method starts at line 841 and your stacktrace shows error in line 683. Timezones database is different and that's because your're getting parse exception.
Upvotes: 4