Reputation: 89
I need to change the SimpleDateFormat
to some other format which is equivalent in jodatime.
Here's the code which needs to be changed.
public static String dateFormat(Date date, String format)
{
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(date);
}
I have tried to use 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 am getting Error.
Upvotes: 7
Views: 11932
Reputation: 44071
Well, I look for you in the documentation of Joda Time and SimpleDateFormat
. As you can see there, the pattern definitions are unfortunately not the same. If you translate from SimpleDateFormat
to Joda-DateTimeFormat, then you have to note following items:
Change Y to x (so called week-year).
Change y to Y (year-of-era) and maybe changing the chronology, too (from ISO to Gregorian/Julian)
W is not supported in Joda Time (week of month), hence no replacement!
F is not supported in Joda Time (day of week in month), hence no replacement!
Change u to e (day number of week - ISO order, not localized), available since Java 7.
The symbol S is handled differently (I suppose in Joda Time better because of correct zero padding).
The zone symbol z is in Joda Time not allowed for parsing (maybe this is the current cause of your problems - you have not shown your pattern format or your exception yet).
The zone/offset symbol Z is handled better in Joda Time, for example allows colons in offset etc. If you need latter you can use X in SimpleDateFormat which has the replacement Z in Joda Time.
Some tests added:
Following sample code demonstrates the different handling of format symbol S.
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 format symbol z (is that 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 do this zone name parsing (although at least dangerous sometimes):
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
Upvotes: 13