首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将UTC日期转换为不同时区的日期

将UTC日期转换为不同时区的日期
EN

Stack Overflow用户
提问于 2019-04-17 11:36:53
回答 3查看 806关注 0票数 0

我收到的日期表示utc中的日期时间。假设:21-6月-2019 10:00

我想把这个日期转换成时区“欧洲/维也纳”,期望:21-6月-2019 12:00

我不明白,为什么下面的代码显示的时间是相同的?

代码语言:javascript
复制
        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()));

输出:

代码语言:javascript
复制
2019-06-21 00:00
2019-06-21 00:00

什么是最好的java7只(没有joda,本地化请)解决方案!?谢谢

编辑:我也尝试过:

代码语言:javascript
复制
        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));

输出:

代码语言:javascript
复制
2019-06-21 00:00
2019-06-21 00:00

溶液

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

代码语言:javascript
复制
        // 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);
EN

回答 3

Stack Overflow用户

发布于 2019-04-17 12:10:34

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

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

代码语言:javascript
复制
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);
票数 1
EN

Stack Overflow用户

发布于 2019-04-17 12:33:37

现代(java.time)溶液

按照您的要求,下面有一个旧API的版本,但为了完整起见,我还将提供更现代的解决方案。如果更新Java不是一种选择,我建议您查看:

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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年):

代码语言:javascript
复制
2019-06-21 12:00
票数 1
EN

Stack Overflow用户

发布于 2019-04-17 11:48:00

请使用SimpleDateFormat转换时区

代码语言:javascript
复制
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));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55726699

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档