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