Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Joda-Time DateTime - ISO 8601 format date to another date format

Tags:

java

jodatime

Inside my Java app I am using Joda-Time to convert the app user entered date from MM/dd/yyyy to ISO 8601 format in order to save it in DB.

Can someone please tell me how I can convert the ISO 8601 date back to MM/dd/yyyy format using Joda-Time?

My code convert user date to ISO 8601 date format:

String date1 = "05/05/2013";
DateTimeFormatter parser1 = DateTimeFormat.forPattern("MM/dd/yyyy");
DateTime dateTimeObj1 = DateTime.parse(date1,parser1);
DateTimeFormatter isoDateFormat = ISODateTimeFormat.dateTime();
String isoDateStr = isoDateFormat.print(dateTimeObj1);
System.out.println(isoDateStr);
like image 345
MChan Avatar asked Feb 02 '14 01:02

MChan


1 Answers

Use Same Formatter

You use the same DateTimeFormatter object to parse as to print (render a string) in Joda-Time 2.3.

Time Zone

Note that your code neglected to address a time zone. In that case you get the JVM's default time zone. Not a good practice.

A DateTime represents both a date and a time. When parsing a string for only the date portion, the time portion is automatically set to first moment of the day. That first moment varies by time zone. So applying a different time zone gives a different result, a different point along the timeline of the Universe, a different milliseconds-since-epoch.

Note the call to withZone when defining the formatter.

Strings

Keep in mind that DateTime objects are not Strings. You can generate a string representation of the date-time information contained inside a DateTime by either:

  • Call the toString method on the DateTime instance.
    Every DateTime has a built-in ISO 8601 formatter, used automatically by the "toString" method.
  • Instantiate your own DateTimeFormatter instance.

Both of these string-generation techniques are seen in the example code below.

Example Code

// Usually better to specify a time zone than rely on default.
DateTimeZone timeZone = DateTimeZone.forID( "Asia/Hong_Kong" );
DateTimeFormatter formatter = DateTimeFormat.forPattern( "MM/dd/yyyy" ).withZone( timeZone );

// Parse string into a DateTime. Define the format.
String input = "05/05/2013";
DateTime dateTime = formatter.parseDateTime( input ); // Defaults to first moment of the day.

// Render date-time as an ISO 8601 string. The "toString" method on DateTime defaults to a built-in ISO 8601 formatter.
// A DateTime object is not itself a string. But a DateTime can generate a string by calling its "toString" method.
String iso8601String = dateTime.toString();

// Parse string into a DateTime. Passing to constructor conveniently uses the built-in ISO 8601 parser built into DateTime class.
DateTime dateTime2 = new DateTime( iso8601String, timeZone );

// Render date-time as a string in a particular format.
String output = formatter.print( dateTime2 );

Rather than hard-code a specific format, you can soft-code a localized format.

String outputUS = DateTimeFormat.forStyle( "S-" ).withLocale( Locale.US ).print( dateTime2 );
String outputQuébécois = DateTimeFormat.forStyle( "F-" ).withLocale( Locale.CANADA_FRENCH ).print( dateTime2 );

Dump to console…

System.out.println( "dateTime: " + dateTime ); // Implicit call to "toString" method in DateTime class generates a new string using a built-in formatter for ISO 8601 format.
System.out.println( "iso8601String: " + iso8601String );
System.out.println( "dateTime2: " + dateTime2 ); // Another implicit call to "toString" method on DateTime class. Generates a new string in ISO format.
System.out.println( "output: " + output );

When run…

dateTime: 2013-05-05T00:00:00.000+08:00
iso8601String: 2013-05-05T00:00:00.000+08:00
dateTime2: 2013-05-05T00:00:00.000+08:00
output: 05/05/2013

String Is Not a Date-Time

Do not think of date-time objects as strings.

A DateTime has no format. That class can parse a String in ISO 8601 format to instantiate a date-time object. Likewise a DateTimeFormatter can parse a String to instantiate a date-time object.

Going the opposite direction, a DateTime has a toString implementation that generates a String representation of the date-time object’s value. Likewise a DateTimeFormatter can generate a String representation of the date-time object’s value.

In all these cases the String representation is entirely different and separate from the date-time object.

like image 181
Basil Bourque Avatar answered Oct 18 '22 19:10

Basil Bourque