Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I figure out the UTC offset using Joda?

Tags:

java

jodatime

I'd like to end up with a string in the form of -5:00 (if we're in NY, for example). What's the best way to go about doing this using Joda?

like image 848
LuxuryMode Avatar asked Jun 05 '12 15:06

LuxuryMode


1 Answers

Not sure if it's the best way, but here's one method:

DateTimeFormatter dtf = DateTimeFormat.forPattern("ZZ");

DateTimeZone zone;

zone = DateTimeZone.forID("America/Los_Angeles");
System.out.println(dtf.withZone(zone).print(0));  // Outputs -08:00

zone = DateTimeZone.forOffsetHoursMinutes(-5, 0);
System.out.println(dtf.withZone(zone).print(0)); // Outputs -05:00

DateTime dt = DateTime.now();
System.out.println(dtf.print(dt)); // Outputs -05:00 (time-zone dependent)

The example you give doesn't include the leading zero on the hours, though. If that's what you're really asking (how to exclude the leading zero), then I'm no help.

like image 52
csd Avatar answered Oct 12 '22 09:10

csd