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
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.
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 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.
Joda-Time provides a comprehensive formatting system. There are two layers: High level - pre-packaged constant formatters. Mid level - pattern-based, like SimpleDateFormat.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With