What is the right way to format Time between different timezones?

◇◆丶佛笑我妖孽 提交于 2019-12-11 01:58:42

问题


I want to format time like 19:19:00 to different time zones. If I use SimpleDateFormat it always takes into account the start of the epoch: 1970.01.01.

Some timezones have different offsets on the start of the epoch and now. For example, the default offset from Europe/Kiev now is UTC+0200 but in 1970 it was UTC+0300. That means if I run my server under Europe/Kiev the client which login under Europe/Berlin(UTC+0100) will see three hours different instead of two.

I can solve this problem by writing a custom formatter for java.sql.Time. But I want to ask maybe there are some common approach or Java tools/libraries which can solve it.

Another solution can be using joda-time:

TimeZone.setDefault(TimeZone.getTimeZone("Europe/Kiev"));
DateTimeZone.setDefault(DateTimeZone.forID("Europe/Kiev"));

DateTimeFormat.forPattern("HH:mm:ss.SSS")
   .withZone(DateTimeZone.forID("Europe/Berlin"))
   .print(Time.valueOf("19:00:00").getTime());

回答1:


You can't format just a time to different time zones. You need a date.

If you want to assume that the date of that time is today, you can try this code:

ZoneId originalZone = ZoneId.of("Europe/Kiev");
ZoneId targetZone = ZoneId.of("Europe/Berlin");
LocalTime originalTime = LocalTime.parse("19:19:00");
LocalTime convertedTime = LocalDate.now(originalZone)
                            .atTime(originalTime)
                            .atZone(originalZone)
                            .withZoneSameInstant(targetZone)
                            .toLocalTime();
System.out.println(convertedTime);



回答2:


Is java.time.instant an alternative for you? It handles all Timestamps internally as UTC-Time.

One way to parse it from a string is Instant.parse("2018-05-30T19:00:00")

If you want to have the time for a specific timezone you can get it with myInstant.atZone("Zone")




回答3:


    ZoneId originalZone = ZoneId.of("Europe/Kiev");
    ZoneId targetZone = ZoneId.of("Europe/Berlin");
    LocalDate assumedDate = LocalDate.now(originalZone);
    String formattedTime = assumedDate.atTime(LocalTime.parse("19:19:00"))
            .atZone(originalZone)
            .withZoneSameInstant(targetZone)
            .format(DateTimeFormatter.ofPattern("HH:mm:ss"));
    System.out.println(formattedTime);

Today this printed:

18:19:00

When you know the date, you should of course use that instead of just today. In the case of Kyiv and Berlin I think they follow the same rules for summer time (DST), so the precise date may not be important. If converting between zones that don’t use the same transitions, or between a time zone that uses summer time and one that doesn’t, it’s suddenly crucial. And who knows in which of those two countries the politicians will change the rules next year? Better be safe.

Link: Oracle tutorial: Date Time explaining how to use java.time.



来源:https://stackoverflow.com/questions/50601391/what-is-the-right-way-to-format-time-between-different-timezones

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!