mlumeau
mlumeau

Reputation: 811

JodaTime - Display time in its own timezone

My app is using JodaTime to manage date parsing and formatting.

I have this timestamp: 2018-07-24T15:30:00-07:00. How can display it as 3:30pm, regardless of the user's whereabouts?

Upvotes: 0

Views: 32

Answers (1)

Meno Hochschild
Meno Hochschild

Reputation: 44061

Following code will print "3:30pm":

DateTimeFormatter iso = ISODateTimeFormat.dateTimeParser().withOffsetParsed();
DateTime tsp = iso.parseDateTime("2018-07-24T15:30:00-07:00");

DateTimeFormatter out = DateTimeFormat.forPattern("h:mma").withLocale(Locale.ENGLISH);

System.out.println(tsp); // 2018-07-24T15:30:00.000-07:00
System.out.println(out.print(tsp).toLowerCase()); // 3:30pm

The main problem is just that the parser does not retain the parsed offset of -7:00 but shifts it to your system timezone unless you also call withOffsetParsed().

Upvotes: 1

Related Questions