Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
393 views
in Technique[技术] by (71.8m points)

java - Convert ZonedDatetime to String

I am receiving JSON object containing a time in following format.

...
"start_time": "2020-12-30T17:30:00Z",
...

I've parsed it and assigned to ZonedDateTime

ZonedDateTime start_time = ZonedDateTime.parse(jsonObject.getString("start_time"));

Now I'd like to show it in String with users' device's local time zone.

I believe json time is in UTC zone. So users in Hongkong would see +8 offset, while users in Los Angeles would see -8 offset. So they would see as follows.

Users in HK: 2020-12-31 01:30

Users in LA: 2020-12-30 09:30


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If those devices had configured the time zone correctly, you can use

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm");
System.out.println(start_time.format(formatter.withZone(ZoneId.systemDefault())));

to get your result


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...