In joda time, how to convert timezone without changing time

I get the UTC timestamp from the database that I set to the JodaTime DateTime instance

 DateTime dt = new DateTime(timestamp.getTime()); 

Saves a time other than 10:00 AM , but with a local time zone. For example, I am in the IST time zone, which is +5: 30 from UTC

I tried a lot of things to change the time zone, but with each thing, it changes the time from 10:00 AM to something else, using the difference of +5: 30

Is there any way I can change TimeZone without affecting the current time

EDIT: If my current time is:

 2013-09-25 11:27:34 AM UTC 

Below is the result when I use this new DateTime(timestamp.getTime());

 2013-09-25 11:27:34 AM Asia/Kolkata 

And here is the result when I use this new DateTime(timestamp.getTime(), DateTimeZone.UTC) ;

 2013-09-25 05:57:34 AM UTC 
+53
java datetime jodatime
Sep 25 '13 at 10:54 on
source share
7 answers

You can use the LocalDateTime class

 LocalDateTime dt = new LocalDateTime(t.getTime()); 

and convert LocalDateTime to DateTime

 DateTime dt = new LocalDateTime(timestamp.getTime()).toDateTime(DateTimeZone.UTC); 

Joda DateTime processes any time in milliseconds, for example, millis from the 1970s in the current time zone. ”Thus, when you create an instance of DateTime it is created with the current time zone.

+46
Sep 25 '13 at 11:21
source share

You can use the withZoneRetainFields() DateTime method to change the time zone without changing the digits in the date.

+45
Sep 25 '13 at 15:36
source share

If your timestamp is: 2015-01-01T00: 00: 00.000-0500 (this is local time [for me])

Try the following:

 DateTime localDt = new DateTime(timestamp.getTime()) .withZoneRetainFields(DateTimeZone.UTC) .withZone(DateTimeZone.getDefault()); 

2014-12-31T19: 00: 00.000-05: 00

Destruction: This gives you a DateTime that matches your label, indicating that it is in UTC:

 new DateTime(timestamp.getTime()) .withZoneRetainFields(DateTimeZone.UTC) 

2015-01-01T00: 00: 00.000Z

This gives you a DateTime, but with the time converted to your local time:

 new DateTime(timestamp.getTime()) .withZoneRetainFields(DateTimeZone.UTC) .withZone(DateTimeZone.getDefault()); 

2014-12-31T19: 00: 00.000-05: 00

+12
Sep 09 '15 at 14:56
source share

Here is how I do it:

 private DateTime convertLocalToUTC(DateTime eventDateTime) { // get your local timezone DateTimeZone localTZ = DateTimeZone.getDefault(); // convert the input local datetime to utc long eventMillsInUTCTimeZone = localTZ.convertLocalToUTC(eventDateTime.getMillis(), false); DateTime evenDateTimeInUTCTimeZone = new DateTime(eventMillsInUTCTimeZone); return evenDateTimeInUTCTimeZone.toDate(); } 
+2
Apr 30 '15 at 7:48
source share

I have the same problem. After reading this set of useful answers and considering my specific needs and available objects, I solved this problem with another DateTime constructor:

 new DateTime("2012-04-23T18:25:46.511Z", DateTimeZone.UTC) 
+1
Apr 08 '18 at 12:05
source share

None of the above answers actually explained the problem. The real problem is that the initial assumptions were incorrect. The timestamp from the database was created using the JVM Asia/Kolkata local time zone and not UTC. This is the default behavior for JDBC, so he still recommends setting the JVM time zone to UTC.

If the timestamp from the database was actually:

 2013-09-25 11:27:34 AM UTC 

Or in ISO-8601 format:

 2013-09-25T11:27:34Z // The trailing 'Z' means UTC 

Then using new DateTime(timestamp, DateTimeZone.UTC) works fine. Look at yourself:

 Timestamp timestamp = new Timestamp(1380108454000L); DateTime dt = new DateTime(timestamp.getTime(), DateTimeZone.UTC); System.out.println(dt); // => 2013-09-25T11:27:34.000Z 

If you're curious where I got 1380108454000L , I just used the Joda parsing classes:

 ISODateTimeFormat.dateTimeParser().parseMillis("2013-09-25T11:27:34Z") 

Alternatively there are websites where you can enter the date, time and time zone, and it returns the epoch value in milliseconds or vice versa. This is sometimes good as a sanity check.

 // https://www.epochconverter.com Input: 1380108454000 Click: "Timestamp to Human Date" Assuming that this timestamp is in milliseconds: GMT: Wednesday, September 25, 2013 11:27:34 AM 

Also, keep in mind that the java.sql.Timestamp class roughly corresponds to the Joda / Java 8+ Instant class. It is sometimes easier to convert between equivalent classes to detect errors like before.

+1
Jul 23 '19 at 5:26
source share

I also have a different approach that helped me a lot. I wanted the workflow thread to be temporarily changed to a specific time zone (saving time), and then when my code ends, I set the original time zone again. It turns out when you use joda libraries do:

 TimeZone.setDefault(TimeZone.getTimeZone(myTempTimeZone)); TimeZone.setDefault(timeZone); 

This is not enough. We also need to change TimeZone to DateTimeZone as follows:

 @Before public void setUp() throws Exception { timeZone = TimeZone.getDefault(); dateTimeZone = DateTimeZone.getDefault(); } @After public void tearDown() throws Exception { TimeZone.setDefault(timeZone); DateTimeZone.setDefault(dateTimeZone); } @Test public void myTest() throws Exception { TimeZone.setDefault(TimeZone.getTimeZone(myTempTimeZone)); DateTimeZone.setDefault(DateTimeZone.forID(myTempTimeZone)); //TODO // my code with an specific timezone conserving time } 

Hope this helps someone else too.

0
Mar 05 '17 at 19:48
source share



All Articles