Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In joda time how to convert time zone without changing time

I am getting UTC timestamp from database which is I am setting into a JodaTime DateTime instance

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

It stores the time perfectly say 10:00 AM but with local time zone. E.g, I am in IST time zone which is +5:30 from UTC

I have tried lots of things for changing the timezone but with every thing it changes the time from 10:00 AM to something else by using +5:30 difference

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

EDIT: If my current time is:

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

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

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

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

2013-09-25 05:57:34 AM  UTC 
like image 288
Abhi Avatar asked Sep 25 '13 10:09

Abhi


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.

How do I convert DateTime to another time zone?

To do this, we will use the FindSystemTimeZoneById() of TimeZoneInfo class. This function takes Id as a parameter to return the equivalent time in timezone passed. In the example below, we are taking the current system time and converting it into “Central Standard Time”. DateTime currentTime = TimeZoneInfo.

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 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

You can use class LocalDateTime

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

and convert LocalDateTime to DateTime

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

Joda DateTime treats any time in millis like "millis since 1970y in current time zone". So, when you create DateTime instance, it is created with current time zone.

like image 197
Ilya Avatar answered Sep 20 '22 11:09

Ilya