Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the SimpleDateFormat to jodatime?

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.

like image 481
Sabarish K Avatar asked Jan 15 '14 11:01

Sabarish K


People also ask

What can I use instead of SimpleDateFormat?

DateTimeFormatter is a replacement for the old SimpleDateFormat that is thread-safe and provides additional functionality.

How do I change the date format in Joda-time?

How to change the SimpleDateFormat to jodatime? String s = "2014-01-15T14:23:50.026"; DateTimeFormatter dtf = DateTimeFormat. forPattern("yyyy-MM-dd'T'HH:mm:ss. SSSS"); DateTime instant = dtf.

What is Jodatime?

Joda-Time is an API created by joda.org which offers better classes and having efficient methods to handle date and time than classes from java. util package like Calendar, Gregorian Calendar, Date, etc. This API is included in Java 8.0 with the java.

How to create new DateTime in java?

To create a datetime object representing a specific date and time, you may use an initialization string: DateTime dt = new DateTime("2004-12-13T21:39:45.618-08:00"); The initialization string must be in a format that is compatible with the ISO8601 standard.


1 Answers

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
like image 104
Meno Hochschild Avatar answered Oct 09 '22 19:10

Meno Hochschild