【问题标题】:Spring @DateTimeFormat is changing timezone while converting @PathVariable to DateSpring @DateTimeFormat 正在更改时区,同时将 @PathVariable 转换为 Date
【发布时间】:2016-10-04 14:45:27
【问题描述】:

当使用@DateTimeFormat 获取日期为@PathVariable 时,我正在尝试使用spring 的自动字符串到日期转换。自动转换完成,但由于某种原因,日期被转换为我过去的日期减去四个小时(复活节夏令时,服务器和客户端都位于)。

例子:

URL:.../something/04-10-2016/somename 将产生值为 2016/10/03 20:00:00 的 someDate 对象(即前一天晚上 8 点,比我过去的日期晚 4 小时。)

如何避免这种自动时区转换。我不需要任何时区信息,这种转换打破了我的预期。

这是我的代码:

@RequestMapping(value = "/something/{someDate}/{name}", method = RequestMethod.GET, headers = "Accept=application/json")
@ResponseBody
public SomeObject getDetails(@PathVariable @DateTimeFormat(pattern = "dd-MM-yyyy") Date someDate,
    @PathVariable String name) {
    // date here comes as GMT - 4
    // more code here
    // return someObject;
}

现在,我通过将路径变量读取为字符串并使用 SimpleDateFormatter 手动将字符串转换为日期来解决此问题。

知道如何在没有时区转换的情况下进行自动转换吗?

【问题讨论】:

    标签: java spring rest spring-mvc datetime-format


    【解决方案1】:

    问题的原因可能是时区,java.util.Date 即时时间被转换回东部时区,因此您会看到该值。如果您使用的是 Java 8,LocalDate 应该可以为您解决问题。

    @PathVariable @DateTimeFormat(pattern = "dd-MM-yyyy") LocalDate date
    

    下面为你整理一些选项

    https://www.petrikainulainen.net/programming/spring-framework/spring-from-the-trenches-parsing-date-and-time-information-from-a-request-parameter/

    【讨论】:

    • 这个我没试过;但即使它有效,我仍然必须将LocalDate 转换为Date,因为我使用的 API 需要 Date 对象。这几乎和使用 SimpleDateFormatter 一样好。
    【解决方案2】:

    Do Nhu Vy 的answer 指出了正确的方向;但我不得不删除它,而不是添加时区信息。

    TimeZone.setDefault(TimeZone.getTimeZone("UTC"));

    从配置中删除它并单独保留默认时区。这对我有用。

    【讨论】:

    • 嗨 Suraj,你能分享 Do Nhu 发表的答案的 URL
    【解决方案3】:

    我遇到了同样的问题,我只在我的序列化程序文件中添加了 UTC。我的意思是,而不是:

    private static final DateTimeFormatter FORMATTER = ISODateTimeFormat.dateHourMinute();
    

    我用过:

    private static final DateTimeFormatter FORMATTER = ISODateTimeFormat.dateHourMinute().withZoneUTC();
    

    这是完整的 JsonJodaDateTimeSerializer.java 的最终代码:

    package cat.meteo.radiosondatge.commons;
    
    import com.fasterxml.jackson.core.JsonGenerator;
    import com.fasterxml.jackson.core.JsonProcessingException;
    import com.fasterxml.jackson.databind.JsonSerializer;
    import com.fasterxml.jackson.databind.SerializerProvider;
    import java.io.IOException;
    import org.joda.time.DateTime;
    import org.joda.time.format.DateTimeFormatter;
    import org.joda.time.format.ISODateTimeFormat;
    
    /**
     * When passing JSON around, it's good to use a standard text representation of
     * the date, rather than the full details of a Joda DateTime object. Therefore,
     * this will serialize the value to the ISO-8601 standard:
     * <pre>yyyy-MM-dd'T'HH:mm:ss.SSSZ</pre> This can then be parsed by a JavaScript
     * library such as moment.js.
     */
    public class JsonJodaDateTimeSerializer extends JsonSerializer<DateTime> {
    
        private static final DateTimeFormatter FORMATTER = ISODateTimeFormat.dateHourMinute().withZoneUTC();
    
        @Override
        public void serialize(DateTime value, JsonGenerator gen, SerializerProvider arg2)
                throws IOException, JsonProcessingException {
    
            gen.writeString(FORMATTER.print(value) + 'Z');
        }
    
    }
    

    此代码摘自Serializing Joda DateTime with Jackson and Spring

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-01
      • 1970-01-01
      • 2011-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-04
      • 2011-07-01
      相关资源
      最近更新 更多