java - Spring @DateTimeFormat 夏令时转换错误

标签 java spring-mvc jodatime dst

我在配置了夏令时(美国/圣保罗时区)的机器上使用 Spring MVC。在我的表单类中,我使用注释 DateTimeFormat 来配置我的日期的输出:

public class JustificativaOcorForm {
  ...
  @NotNull
  @DateTimeFormat(pattern="yyyy-MM-dd")
  private Date dataMarcacao;
  ...
}

在调试时,我得到的日期是 16/10/2011 (dd/MM/yyyy),这是夏令时的开始,但 Spring 将其转换为 2011-10-15。为什么?

2011-11-04 16:35:31,965 [http-8080-Processor25] DEBUG org.springframework.core.convert.support.GenericConversionService - Converting value Sun Oct 16 00:00:00 BRST 2011 of [TypeDescriptor @javax.validation.constraints.NotNull @org.springframework.format.annotation.DateTimeFormat java.util.Date] to [TypeDescriptor java.lang.Long]
2011-11-04 16:35:31,965 [http-8080-Processor25] DEBUG org.springframework.core.convert.support.GenericConversionService - Converted to 1318730400000
2011-11-04 16:35:32,010 [http-8080-Processor25] DEBUG org.springframework.core.convert.support.GenericConversionService - Converted to '2011-10-15'

我看到这个问题:@DateTimeFormat in Spring produces off-by-one day error

但是 Spring 3 使用 Joda-Time,而我的类路径中有 joda-time-2.0.jar,所以我不知道为什么会发生这种情况以及如何解决它。

[编辑]

我测试了创建 LocalData 对象,并发现了一些东西:

LocalDate ld = new LocalDate( new SimpleDateFormat("dd/MM/yyyy").parse("16/10/2011").getTime() );
System.out.println( new SimpleDateFormat("dd/MM/yyyy HH:mm:ss Z z").format( ld.toDate() )  );
//prints 15/10/2011 00:00:00 -0200 BRST

LocalDate ld2 = new LocalDate( 2011,10,16 );
System.out.println( new SimpleDateFormat("dd/MM/yyyy HH:mm:ss Z z").format( ld2.toDate() )  );
//prints 16/10/2011 00:00:00 -0200 BRST

似乎第一种方法认为时间是 UTC,因为调试我可以看到 Joda 使用类 DateTimeZone 的方法 convertUTCToLocal。

也许这也是 Spring 的默认设置,他也希望有一个 UTC 日期,而我正在传递 BRT 日期。

所以我认为我的解决方案是将对象更改为 LocalDate 并使用第二种方法创建该对象的实例。

最佳答案

这可能会回答您的部分问题。

当你有一个 java.util.Data 对象时,它会在你的系统时区内打印到 toString 中。因此,如果您设置的日期为 UTC 2011-10-16 00:00:00,该日期将在日期内部转换为 UTC 时间戳。 toString 将打印您本地时区的时间戳,这将是世界标准时间之后几个小时(因为圣保罗位于伦敦西部),大约是 2011 年 10 月 15 日 22:00:00。这就是您将在断点和调试打印中看到的内容。不过,数据在内部可能仍然是正确的。

如果发现打印日期的唯一真正方法是像这样通过 SimpleDateFormat:

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    try {
        Date dateInUTC = dateFormat.parse("2011-10-16 00:00:00");
        Date currentDate = new Date();
        String stringInUTC = dateFormat.format(currentDate);
        System.out.println(dateInUTC);
        System.out.println(currentDate);
        System.out.println(stringInUTC);
    }
    catch (ParseException e) {
        // not too worry, I wrote a nice date
    }

现在打印品看起来会很困惑

Sun Oct 16 01:00:00 CET 2011
Thu Nov 10 15:47:46 CET 2011
2011-11-10 14:47:46

但是让我们走一遍。

  1. 首先,我的 SimpleDateFormat 获得一种格式,我将其设置为假设所有输入和输出的文本都在 UTC 时区。
  2. 当我解析日期 2011-10-16 00:00:00 时,它将被解释为 UTC,但当我打印它时,java 使用我的语言环境 (CET) 将其打印为 2011-10-16 01:00:00 ,这也是我在断点上看到的
  3. 当我创建一个新的 Date() 时,该日期将在我的语言环境中,当我打印它时,它将显示 15:47(我的当前时间),但是当我使用我的时区感知 SimpleDateFormat 对其进行格式化时,它将显示时间协调世界时。

就是说,java 和日期会让你感到困惑,直到你把头发拔出来:)

希望这对一些人有帮助。

关于java - Spring @DateTimeFormat 夏令时转换错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8014690/

相关文章:

java.lang.UnixProcess 在 NTFS 上执行 `find` 本质上很慢?

Java 数组 - 来自测试

java - 为什么请求会发送到 Struts Dispatcher?

java - Joda setJulian 非法参数异常

java - 单例和其他设计问题

java - 如何使用 Apache Common fileupload 将上传文件的路径设置为 "context path"?

java - 在 Spring MVC 中将 ContextLoaderListener 添加到 web.xml

java - Spring+JPA+MVC(无Maven)创建bean时出错

java - 在 joda time 中使用 parseDateTime 时休息 1 天

java - 时间无关的 JUnit 测试用例