How to change SimpleDateFormat to jodatime?

I need to change SimpleDateFormat to another format that is equivalent to jodatime. Here is the code to change.

 public static String dateFormat(Date date, String format) { SimpleDateFormat sdf = new SimpleDateFormat(format); return sdf.format(date); } 

I tried using DateTimeFormatter.

 public static String dateFormat(DateTime date, String format) { DateTimeFormatter dtf = DateTimeFormat.forPattern(format); DateTime tempDateTime = dtf.parseDateTime(date.toString()); return tempDateTime.toString(); } 

But I get an error message.

+6
source share
1 answer

Ok, I'm looking for you in the documentation for Joda's time and SimpleDateFormat . As you can see, the template definitions, unfortunately, do not match. If you are translating from SimpleDateFormat to Joda- DateTimeFormat , you should note the following points:

Change Y to x (the so-called weekly year).

Change y to Y (throughout the year) and possibly also change the chronology (from ISO to Gregorian / Julian)

W is not supported in Joda Time (week of the month), therefore there is no replacement!

F is not supported in Joda Time (day of the week in the month), therefore there is no replacement!

Change u to e (day of the week is an ISO order, not localized), available with Java 7.

The S character is handled differently (I believe Joda Time is better due to the correct zero padding).

The z zonal character is in Joda Time, which is not allowed for parsing (perhaps this is the current cause of your problems - you have not yet specified your template or your exception ).

The Z character / Z offset is best handled during Joda, for example, allowing colons to be offset, etc. If you need the latter, you can use X in SimpleDateFormat, which has a Z replacement during Joda.

Some tests added:

The following code sample demonstrates various handling of a character in S format.

 String s = "2014-01-15T14:23:50.026"; DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSS"); DateTime instant = dtf.parseDateTime(s); System.out.println(dtf.print(instant)); // 2014-01-15T14:23:50.0260 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSS"); Date date = sdf.parse(s); System.out.println(sdf.format(date)); // 2014-01-15T14:23:50.0026 (bad!) 

Another test for a z character (is this your problem ???):

 String s = "2014-01-15T14:23:50.026PST"; DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSz"); DateTime instant = dtf.parseDateTime(s); System.out.println(dtf.print(instant)); // abort Exception in thread "main" java.lang.IllegalArgumentException: Invalid format: "2014-01-15T14:23:50.026PST" is malformed at "PST" at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:866) at time.JodaTest8.main(JodaTest8.java:83) 

SimpleDateFormat can parse the name of this zone (although at least sometimes it is dangerous):

 String s = "2014-01-15T14:23:50.026PST"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSz"); Date date = sdf.parse(s); System.out.println(sdf.format(date)); // 2014-01-15T14:23:50.026PST 
+12
source

All Articles