获取不同时区的相同日期

时间:2019-11-21 09:34:57

标签: java date timezone localdate zoneddatetime

我的时间是秒。 从这个时代开始创建日期对象。

Date date = new Date(epochMilli);

假设日期为"23 Nov 2019 00:00:00"

现在,我只想在不同的时区中获得相同的日期,例如:]

日本时间:2019年11月23日00:00:00

美国时间:2019年11月23日00:00:00

我当前正在使用LocalDateTimeZonedDateTime

但是当我转换到其他区域时,时间也会改变。但是我不希望这次改变。

谢谢。

2 个答案:

答案 0 :(得分:4)

不幸的是,您没有向我们展示您使用ZonedDateTime的方式 ,因此以下示例通过展示如何解析毫秒,转换结果日期时间,可能会超出您的预期范围从一个区域到另一个区域,以及如何使用不同的区域解析日期时间:

public static void main(String[] args) {
    long epochMillis = 1574208000000L;

    // define a formatter to be used
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd MMM yyyy HH:mm:ss");

    /*
     * Part 1: Getting the date time converted to different time zones
     */

    // parse the millis once using a specific time zone, here: UTC
    ZonedDateTime utcFromMillis = ZonedDateTime.ofInstant(Instant.ofEpochMilli(epochMillis), 
                                                            ZoneId.of("UTC"));

    // then take the UTC-ZonedDateTime as base for conversion to other time zones 
    ZonedDateTime asiaTokyoConvertedfromUtc = utcFromMillis.toInstant()
            .atZone(ZoneId.of("Asia/Tokyo"));
    ZonedDateTime americaLosAngelesConvertedfromUtc = utcFromMillis.toInstant()
            .atZone(ZoneId.of("America/Los_Angeles"));
    ZonedDateTime americaChicagoConvertedfromUtc = utcFromMillis.toInstant()
            .atZone(ZoneId.of("America/Chicago"));

    // print the results
    System.out.println("#### 1574208000000L at UTC, converted to other zones ####");
    System.out.println("UTC time zone:\t\t\t\t" + utcFromMillis.format(dtf));
    System.out.println("JST (Japan/Tokyo) time zone:\t\t"
            + asiaTokyoConvertedfromUtc.format(dtf));
    System.out.println("PST (USA/Los Angeles) time zone:\t"
            + americaLosAngelesConvertedfromUtc.format(dtf));
    System.out.println("CST (USA/Chicago) time zone:\t\t"
            + americaChicagoConvertedfromUtc.format(dtf));

    System.out.println();

    /*
     * Part 2: Getting the date time in different time zones
     */

    // parse the millis using time zone Asia/Tokyo
    ZonedDateTime asiaTokyoFromMillis = ZonedDateTime.ofInstant(
            Instant.ofEpochMilli(epochMillis), 
            ZoneId.of("Asia/Tokyo"));

    // parse the millis using time zone America/Los Angeles
    ZonedDateTime americaLosAngelesFromMillis = ZonedDateTime.ofInstant(
            Instant.ofEpochMilli(epochMillis), 
            ZoneId.of("America/Los_Angeles"));

    // parse the millis using time zone America/Chicago
    ZonedDateTime americaChicagoFromMillis = ZonedDateTime.ofInstant(
            Instant.ofEpochMilli(epochMillis), 
            ZoneId.of("America/Chicago"));

    // print the (expected) results, same as converted date times...
    System.out.println("#### 1574208000000L at different zones ####");
    System.out.println("UTC time zone:\t\t\t\t" + utcFromMillis.format(dtf));
    System.out.println("JST (Asia/Tokyo) time zone:\t\t"
            + asiaTokyoFromMillis.format(dtf));
    System.out.println("PST (USA/Los Angeles) time zone:\t"
            + americaLosAngelesFromMillis.format(dtf));
    System.out.println("CST (USA/Chicago) time zone:\t\t"
            + americaChicagoFromMillis.format(dtf));

    System.out.println();

    /*
     * Part 3: How to parse the date time instead of millis
     */

    // provide a parseable date time String
    String dateTime = "23 Nov 2019 00:00:00";

    // parse it in each desired time zone
    ZonedDateTime utc23Nov2019 = LocalDateTime.parse(dateTime, dtf)
            .atZone(ZoneId.of("UTC"));
    ZonedDateTime asiaTokyo23Nov2019 = LocalDateTime.parse(dateTime, dtf)
            .atZone(ZoneId.of("Asia/Tokyo"));
    ZonedDateTime americaChicago23Nov2019 = LocalDateTime.parse(dateTime, dtf)
            .atZone(ZoneId.of("America/Los_Angeles"));
    ZonedDateTime americaLosAngeles23Nov2019 = LocalDateTime.parse(dateTime, dtf)
            .atZone(ZoneId.of("America/Chicago"));

    // print the results, now you have the 23. Nov 2019 at 00:00:00 in each time zone
    System.out.println("#### \"23 Nov 2019 00:00:00\" at different zones ####");
    System.out.println("UTC time zone:\t\t\t\t" + utc23Nov2019.format(dtf));
    System.out.println("JST (Asia/Tokyo) time zone:\t\t"
            + asiaTokyo23Nov2019.format(dtf));
    System.out.println("PST (USA/Los Angeles) time zone:\t"
            + americaChicago23Nov2019.format(dtf));
    System.out.println("CST (USA/Chicago) time zone:\t\t"
            + americaLosAngeles23Nov2019.format(dtf));
}

它的输出是

#### 1574208000000L at UTC, converted to other zones ####
UTC time zone:                      20 Nov 2019 00:00:00
JST (Japan/Tokyo) time zone:        20 Nov 2019 09:00:00
PST (USA/Los Angeles) time zone:    19 Nov 2019 16:00:00
CST (USA/Chicago) time zone:        19 Nov 2019 18:00:00

#### 1574208000000L at different zones ####
UTC time zone:                      20 Nov 2019 00:00:00
JST (Asia/Tokyo) time zone:         20 Nov 2019 09:00:00
PST (USA/Los Angeles) time zone:    19 Nov 2019 16:00:00
CST (USA/Chicago) time zone:        19 Nov 2019 18:00:00

#### "23 Nov 2019 00:00:00" at different zones ####
UTC time zone:                      23 Nov 2019 00:00:00
JST (Asia/Tokyo) time zone:         23 Nov 2019 00:00:00
PST (USA/Los Angeles) time zone:    23 Nov 2019 00:00:00
CST (USA/Chicago) time zone:        23 Nov 2019 00:00:00

答案 1 :(得分:1)

旧版java.util.Date不支持时区。从1970-1-1 0:00开始,它基本上是一个以毫秒为单位的时间点。没什么。

打印日期时,将使用系统的默认语言环境和时区对日期进行格式化。为了使java.util.Date对象的格式设置为不同的时区,您必须相应地配置DateFormat

Date pointInTime = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance();
dateFormat.setTimeZone(TimeZone.getTimeZone("PST"));
System.out.println(dateFormat.format(pointInTime));
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(dateFormat.format(pointInTime));

使用更新的LocalDate API,情况有所不同。前面的答案提供了一个丰富的示例。