Java – how to find UTC offset using joda?

I want to end with a string in the form of - 5:00 (if we're in New York State) What is the best way to use joda?

Solution

I'm not sure if it's the best way, but here's a way:

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 does not include leading zeros for hours If this is your real problem (how to eliminate leading zeros), then I can't help

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>