Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joda Time parse a date with timezone and retain that timezone

I want to parse a date, which was created with a specific timezone, convert it to a format and return it. The conversion works but the timezone offset is always set to +0000 with the time difference being added/subtracted as necessary. How can I get it to format and keep the offset correct?

I expect this: 2012-11-30T12:08:56.23+07:00

But get this: 2012-11-30T05:08:56.23+00:00

Implementation:

public static final String ISO_8601_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSZZ";  public static String formatDateToISO8601Standard(Date date) {     DateTime dateTime = new DateTime(date);     DateTimeFormatter df = DateTimeFormat.forPattern(ISO_8601_DATE_FORMAT);     return dateTime.toString(df); } 

Test class:

private static final String DATE_WITH_TIMEZONE = "30 11 2012 12:08:56.235 +0700"; private static final String EXPECTED_DATE_WITH_TIMEZONE = "2012-11-30T12:08:56.23+07:00";  @Test public void testFormattingDateWithSpecificTimezone() throws Exception {     String result = JodaDateUtil.formatDateToISO8601Standard(createDate(DATE_WITH_TIMEZONE));     assertEquals("The date was not converted correctly", EXPECTED_DATE_WITH_TIMEZONE, result); }  private Date createDate(String dateToParse) throws ParseException {     DateTimeFormatter df = DateTimeFormat.forPattern("dd MM yyyy HH:mm:ss.SSS Z");     DateTime temp = df.parseDateTime(dateToParse);     Date date = temp.toDate();     return date; } 
like image 520
edwardmlyte Avatar asked May 28 '13 14:05

edwardmlyte


People also ask

Does Joda DateTime have timezone?

Adjusting Time ZoneUse the DateTimeZone class in Joda-Time to adjust to a desired time zone. Joda-Time uses immutable objects. So rather than change the time zone ("mutate"), we instantiate a new DateTime object based on the old but with the desired difference (some other time zone). Use proper time zone names.

Is Joda-time deprecated?

So the short answer to your question is: YES (deprecated).

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

I think this will work, if you are using JodaTime: String strDateTime = "11/15/2013 08:00:00"; DateTime dateTime = DateTime. parse(strDateTime); DateTimeFormatter fmt = DateTimeFormat. forPattern("MM/dd/YYYY"); String strDateOnly = fmt.

What is Joda-time format?

Joda-Time provides a comprehensive formatting system. There are two layers: High level - pre-packaged constant formatters. Mid level - pattern-based, like SimpleDateFormat.


1 Answers

Basically, once you parse the date string [in your createDate() method] you've lost the original zone. Joda-Time will allow you to format the date using any zone, but you'll need to retain the original zone.

In your createDate() method, the DateTimeFormatter "df" can return the zone that was on the string. You'll need to use the withOffsetParsed() method. Then, when you have your DateTime, call getZone(). If you save this zone somewhere or somehow pass it to your formatting routine, then you can use it there by creating a DateTimeFormatter "withZone" and specifying that zone as the one you want on the format.

As a demo, here's some sample code in a single method. Hopefully, it'll help change your code the way you want it to run.

  public static void testDate()    {     DateTimeFormatter df = DateTimeFormat.forPattern("dd MM yyyy HH:mm:ss.SSS Z");     DateTime temp = df.withOffsetParsed().parseDateTime("30 11 2012 12:08:56.235 +0700");     DateTimeZone theZone = temp.getZone();      Date date = temp.toDate();      DateTime dateTime = new DateTime(date);     DateTimeFormatter df2 = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSZZ");     DateTimeFormatter df3 = df2.withZone(theZone);      System.out.println(dateTime.toString(df2));     System.out.println(dateTime.toString(df3));    } 
like image 154
Darius X. Avatar answered Sep 29 '22 17:09

Darius X.