Converting Time Zones from a String to UTC (JODA TIME)

Mr. Octodood :

I have a stored UTC timestamp in the database. When I retrieve that UTC timestamp I cast it into a String. I want to take that UTC Timestamp String and convert it to the device's local time using Joda Time. Anyone who could possibly help out with this. It would be very appreciated! Here is what I am doing right now:

                String date = ""+ds.child("datecreated").getValue();

                DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");

                DateTime dt = formatter.parseDateTime(date);

                DateTime dt2 = new DateTime(dt).toDateTime(DateTimeZone.getDefault());

                String personalDate = dt2.toString();

                dateTV.setText(personalDate);

                System.out.println("THIS IS THE FIRST TIME: " + dt + "THIS IS THE SECOND TIME: " + dt2); 

The problem is is that it is giving me the exact same time when I convert it to my local time, which it shouldn't be doing since it is being stored in UTC and I am converting to Eastern Standard Time which is my phone's default.

Ole V.V. :

To show that Andreas in the comment has hit the nail right on: I ran the following snippet in America/Coral_Harbour time zone (since I didn’t know your exact time zone, Eastern Standard Time is used in several (though fewer after 8 March when Eastern Daylight Time began)).

    String date = "2020-03-12T01:23:45.678+0000";

    System.out.println("This is the string:      " + date); 

    DateTime dt = new DateTime(date);
    DateTime dt2 = new DateTime(dt).toDateTime(DateTimeZone.getDefault());

    System.out.println("This is the first time:  " + dt); 
    System.out.println("This is the second time: " + dt2); 

Output is:

This is the string:      2020-03-12T01:23:45.678+0000
This is the first time:  2020-03-11T20:23:45.678-05:00
This is the second time: 2020-03-11T20:23:45.678-05:00

Compare the first two lines and notice that the conversion from UTC to EST has already happened when parsing the string.

As an aside, since your string is in ISO 8601 format, you don’t need to specify any formatter for parsing it. The DateTime(Object) constructor accepts it. But the same conversion happened in your parsing.

What happened in your code?

Repeating the quote from Andreas’ comment:

If the withOffsetParsed() has been called, then the resulting DateTime will have a fixed offset based on the parsed time zone. Otherwise the resulting DateTime will have the zone of this formatter, but the parsed zone may have caused the time to be adjusted.

So your formatter has the default time zone of your device, and therefore also the DateTime object that you get from parsing.

So when creating dt2 you were converting from Eastern Standard Time to Eastern Standard Time and therefore got the same date-time again.

Link: Documentation of DateTimeFormatter.parseDateTime()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=227855&siteId=1