将UTC日期转换为其他时区的日期

时间:2019-04-17 11:36:53

标签: java date java-7

我收到一个表示utc中日期时间的日期。可以说: 2019年6月21日10:00

我想将此日期时间转换为“欧洲/维也纳”时区,期望: 2019年6月21日12:00

我不明白,为什么我的下面的代码同时显示两个时间

        Date utcFinish = new Date(new Date().getYear(), Calendar.JUNE, 21);
        TimeZone europeVienna = TimeZone.getTimeZone("Europe/Vienna");
        Calendar finishInViennaTime = Calendar.getInstance(europeVienna);
        finishInViennaTime.setTime(utcFinish);

        System.out.println(format.format(utcFinish));
        System.out.println(format.format(finishInViennaTime.getTime()));

输出:

2019-06-21 00:00
2019-06-21 00:00

什么是最好的仅Java7(无joda,localdate pls)解决方案!谢谢

编辑:我也尝试过:

        SimpleDateFormat formatWithTimezone = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        formatWithTimezone.setTimeZone(TimeZone.getTimeZone("Europe/Vienna"));

        SimpleDateFormat formatonly = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        Date utcDate = new Date(new Date().getYear(), Calendar.JUNE, 21);

        System.out.println(formatonly.format(utcDate));
        System.out.println(formatWithTimezone.format(utcDate));

输出:

2019-06-21 00:00
2019-06-21 00:00

解决方案

感谢所有解决方案。最后,问题是默认时区。这是我当前的解决方案(欢迎进一步的反馈!):

        // Unfortunately this date has the wrong time zone (Local Time Zone),
        // because Date assumes Local Time Zone the database stores timestamps 
        // in utc that's why I now convert to a datestring and reparse

        Date finishTimeWrongTimeZone = new Date(new Date().getYear(), Calendar.JUNE, 21);
        // in reality i call the db here like getFinishTime();

        // get the plain date string without time shifting
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MMM-dd HH:mm");
        String dateWithoutTimeZone = formatter.format(finishTimeWrongTimeZone);

        // add the timezone to the formatter and reinterpret the datestring
        // effectively adding the correct time zone the date should be in
        formatter.setTimeZone(TimeZone.getTimeZone("UTC"));

        String finishTime = null;
        try {

            Date dateWithCorrectTimeZone = formatter.parse(dateWithoutTimeZone);

            // Convert to expected local time zone (europe/vienna)
            formatter.setTimeZone(TimeZone.getTimeZone("Europe/Vienna"));
            finishTime = formatter.format(dateWithCorrectTimeZone);

        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(finishTime);

4 个答案:

答案 0 :(得分:1)

在进行格式化之前更改时区。日期将被相应地“转换”,但是正如我们已经告诉您的那样,这种旧的编码风格有很多缺陷:

public static void main(String[] x) {
    Date instant = new Date(new Date().getYear(), Calendar.JUNE, 21); // this call assumes the Timezone is your current default (system dependant).
    DateFormat sdf = SimpleDateFormat.getDateTimeInstance();

    sdf.setTimeZone(TimeZone.getTimeZone("Europe/Vienna"));
    System.out.println(sdf.format(instant)); //21 juin 2019 00:00:00

    sdf.setTimeZone(TimeZone.getTimeZone("Europe/Greenwich"));
    System.out.println(sdf.format(instant)); // 20 juin 2019 22:00:00

}

答案 1 :(得分:1)

我使用一个格式化程序对象并更改其时区

SimpleDateFormat格式=新的SimpleDateFormat(“ dd-MM-yyyy HH:mm”);    format.setTimeZone(TimeZone.getTimeZone(“ UTC”));

Date dUtc = format.parse("21-06-2019 10:00");
System.out.println(dUtc);

TimeZone europeVienna = TimeZone.getTimeZone("europe/vienna");

format.setTimeZone(europeVienna);
String sVienna = format.format(dUtc);
System.out.println(sVienna);

答案 2 :(得分:1)

现代(java.time)解决方案

我按照您的要求为下面的旧API提供了一个版本,但出于完整性考虑,我还将提供更现代的解决方案。如果不能选择更新Java,我建议您研究ThreeTen-Backport:

ZonedDateTime zdt = LocalDateTime.of(
    Year.now().getValue(), Month.JUNE, 21, 10, 0, 0
).atZone(ZoneOffset.UTC);

System.out.println(
    zdt.withZoneSameInstant(ZoneId.of("Europe/Vienna"))
      .format(DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm"))
);

旧(java.util)解决方案

new Date(...)已过时,您不应使用它。如果您真的需要使用旧的API,您需要使用Calendar

Calendar utcFinish = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
utcFinish.set(Calendar.MONTH, Calendar.JUNE);
utcFinish.set(Calendar.DATE, 21);
utcFinish.set(Calendar.HOUR_OF_DAY, 10);
utcFinish.set(Calendar.MINUTE, 0);

然后将DateFormat与您实际希望打印的时区一起使用:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
TimeZone europeVienna = TimeZone.getTimeZone("Europe/Vienna");
format.setTimeZone(europeVienna);

System.out.println(format.format(utcFinish.getTime()));

输出

两种解决方案都应输出(在撰写本文时,于2019年):

2019-06-21 12:00

答案 3 :(得分:0)

请使用SimpleDateFormat转换时区

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date utcFinish = new Date(new Date().getYear(), Calendar.JUNE, 21);
TimeZone europeVienna = TimeZone.getTimeZone("Europe/Vienna");
format.setTimeZone(europeVienna);
System.out.println(format.format(utcFinish));
相关问题