Reputation: 455
I am using joda 2.7, I want to convert
UTC Time= 09:30:10-06:00
to Local Time= 03:30:10
and
UTC Time=10:45:00+07:30
to Local Time = 18:15:00
I tried different formats both joda's and my own and methods to handle the offset part, but I am not able to get the required output. What is the best approach.
Upvotes: 1
Views: 1079
Reputation:
You can do:
DateTimeFormatter fmt = DateTimeFormat.forPattern("HH:mm:ssZ");
String utcTime = "09:30:10-06:00";
DateTime localTime = fmt.parseDateTime(utcTime);
String localTimeString = localTime.toString("HH:mm:ss");
This will convert it to your local time, according to your default timezone (do System.out.println(DateTimeZone.getDefault())
to check what's your default timezone).
If you want to convert to a different timezone, you need to do something like:
DateTimeFormatter fmt = DateTimeFormat.forPattern("HH:mm:ssZ").withZone(/*DateTimeZone you want*/);
In your specific cases, to get the output you want, you just need to use the timezone in which you have the desired outputs:
DateTimeFormatter fmt = DateTimeFormat.forPattern("HH:mm:ssZ").withZone(DateTimeZone.forOffsetHours(-12));
String utcTime = "09:30:10-06:00";
DateTime localTime = fmt.parseDateTime(utcTime);
System.out.println(localTime.toString("HH:mm:ss"));
utcTime = "10:45:00+07:30";
localTime = fmt.withZone(DateTimeZone.forOffsetHours(15)).parseDateTime(utcTime);
System.out.println(localTime.toString("HH:mm:ss"));
Output is:
03:30:10
18:15:00
Upvotes: 1