【问题标题】:Thymeleaf Temporals and dates with TimezoneThymeleaf 时间和日期与时区
【发布时间】:2021-05-20 14:09:43
【问题描述】:

我有一个使用 Thymeleaf 作为视图层的 Spring Boot Web 应用程序,我需要显示一些具有特定时区 (CET = UTC+1/UTC+2) 的日期。

服务器设置为 UTC,所有日期都以 UTC 格式作为日期时间存储在我的数据库中,这很好。

现在我想在我的 HTML 页面上显示这个日期,而不是 UTC 格式,而是使用 Thymeleaf Temporals 的 CET,但它似乎不起作用。

日期对象是一个 Java Instant。

从 DB 中检索到的日期是(例如)2021-02-17T16:18:21Z,显示如下:

<div th:text="${#temporals.format(user.lastAccess, 'dd/MM/yyyy HH:mm')}"></div>
=> 17/02/2021 16:18

但我想显示如下:

17/02/2021 17:18

所以我用了:

<div th:text="${#temporals.format(user.lastAccess, 'dd/MM/yyyy HH:mm', new java.util.Locale('it', 'IT'))}"></div>

但日期始终显示为 UTC

17/02/2021 16:18

Thymeleaf 的 Java8TimeDialect 配置正确。

我正在使用:

Spring Boot 2.2.4
Thymeleaf 3.0.11.RELEASE

我做错了什么?

谢谢。

【问题讨论】:

    标签: spring-boot datetime timezone thymeleaf java-time


    【解决方案1】:

    没有Thymeleaf temporals function 可以为您执行该时区转换。

    Instant 值需要指定时区,而不是语言环境(Java 语言环境不会以这种方式操作时区)。

    你可以在 Java 中做你需要的事情:

    Instant myInstant = Instant.parse("2021-02-17T16:18:00.00Z");
    ZonedDateTime myZDT = myInstant.atZone(ZoneId.of("CET"));
    

    现在,您可以使用以下 Thymeleaf sn-p:

    <div th:text="${#temporals.format(myZDT, 'dd/MM/yyyy HH:mm')}"></div>
    

    这将在div 中生成以下内容:

    17/02/2021 17:18
    

    现在时间显示为17:18,而不是16:18


    更新:请务必阅读 OleV.V 的评论。关于不推荐使用的缩写,包括注释:

    三个字母的时区缩写已被弃用,而且经常模棱两可,所以不要使用 ZoneId.of("CET")。使用时区 ID,例如 Europe/Vienna 或 Europe/San_Marino,因此采用区域/城市格式。

    因此以下问题可能有用:

    Where is the official list of zone names for java.time?

    还有一个相关的代码sn-p:

    Set<String> zoneIds = ZoneId.getAvailableZoneIds();
    for (String zone : zoneIds) {
        System.out.println(zone);
    }
    

    【讨论】:

    • 谢谢@OleV.V。我注意到VV 是时区数据的有效字符串格式化程序。巧合?我不这么认为......
    【解决方案2】:

    您需要将时间戳转换为具有指定时区的日期时间。下面给出了一些这样做的方法。

    使用ZonedDateTime#withZoneSameInstant

    import java.time.ZoneId;
    import java.time.ZonedDateTime;
    
    class Main {
        public static void main(String[] args) {
            String timestamp = "2021-02-17T16:18:00.00Z";
            ZonedDateTime zdt = ZonedDateTime.parse(timestamp);
            ZonedDateTime zdtAtCet = zdt.withZoneSameInstant(ZoneId.of("Europe/Paris"));
            System.out.println(zdtAtCet);
        }
    }
    

    现在,您可以在代码中使用OffsetDateTime 的对应对象,例如

    <div th:text="${#temporals.format(zdtAtCet, 'dd/MM/yyyy HH:mm')}"></div>
    

    ZonedDateTime 旨在自动调整由于夏令时导致的与 UTC 的偏移量变化。

    如果您要处理与 UTC 的固定偏移量,例如UTC+01:00,也可以使用OffsetDateTime#withOffsetSameInstant 例如

    import java.time.OffsetDateTime;
    import java.time.ZoneOffset;
    
    class Main {
        public static void main(String[] args) {
            String timestamp = "2021-02-17T16:18:00.00Z";
            OffsetDateTime odt = OffsetDateTime.parse(timestamp);
            OffsetDateTime odtAtUtcPlus1 = odt.withOffsetSameInstant(ZoneOffset.of("+01:00"));
            OffsetDateTime odtAtUtcPlus2 = odt.withOffsetSameInstant(ZoneOffset.of("+02:00"));
            System.out.println(odtAtUtcPlus1);
            System.out.println(odtAtUtcPlus2);
        }
    }
    

    输出:

    2021-02-17T17:18+01:00
    2021-02-17T18:18+02:00
    

    您也可以使用Instant#atZoneInstant#atOffset 分别执行上述第一个和第二个解决方案,例如

    import java.time.Instant;
    import java.time.OffsetDateTime;
    import java.time.ZoneId;
    import java.time.ZoneOffset;
    import java.time.ZonedDateTime;
    
    class Main {
        public static void main(String[] args) {
            String timestamp = "2021-02-17T16:18:00.00Z";
            Instant instant = Instant.parse(timestamp);
            OffsetDateTime odtAtUtcPlus1 = instant.atOffset(ZoneOffset.of("+01:00"));
            ZonedDateTime zdtAtCet = instant.atZone(ZoneId.of("Europe/Paris"));
            System.out.println(odtAtUtcPlus1);
            System.out.println(zdtAtCet);
        }
    }
    

    输出:

    2021-02-17T17:18+01:00
    2021-02-17T17:18+01:00[Europe/Paris]
    

    Trail: Date Time 了解有关现代日期时间 API 的更多信息。

    【讨论】:

    • 如果有办法以这种方式在 Spring Boot 应用程序中集中处理所有日期,那就太好了。然而,建议的解决方案工作正常。谢谢。
    【解决方案3】:

    Temporals 是自动创建的。采用系统默认时区。

    public Temporals(final Locale locale) {
        this(locale, ZoneId.systemDefault());
    }
    

    创建过程并不是真正设计为可定制的,因此最好的方法似乎是更改系统默认的 ZoneId。

    对我来说,最好的方法是在 Docker 容器中设置预期的时区。就我而言,服务器上的日期已经正确。可以使用将其传播到容器卷:

    -v /etc/timezone:/etc/timezone:ro -v /etc/localtime:/etc/localtime:ro
    

    【讨论】:

      猜你喜欢
      • 2014-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多