java 失去了一天的约会

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31622254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:52:56  来源:igfitidea点击:

lost one day for a date

javajsonHymansonorikaspring-restcontroller

提问by robert trudel

On client side, i use dd/MM/yyyy date format. The field use a twitter bootstrap 3 datetime picker (https://eonasdan.github.io/bootstrap-datetimepicker/)

在客户端,我使用 dd/MM/yyyy 日期格式。该字段使用 twitter bootstrap 3 日期时间选择器 ( https://eonasdan.github.io/bootstrap-datetimepicker/)

I enter via twitter bootstrap 3 datetime picker 24/07/2015
in my json i sent, i see: birthdate: "24/07/2015"

我通过 twitter bootstrap 3 日期时间选择器 24/07/2015
在我发送的 json 中输入,我看到:生日:“24/07/2015”

In my dto, i do

在我的 dto 中,我做

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy")
private Date birthdate;

When i receive the date on the server, in my dto see: 23/07/2015 19:00

当我在服务器上收到日期时,在我的 d 中看到:23/07/2015 19:00

One day is lost.

失去了一天。

Any explication?

有什么解释吗?

回答by AimZ

According to HymansonFAQDateHandlingpage:

根据HymansonFAQDateHandling页面:

All time objects that have associated TimeZone (java.util.Calendar etc) that Hymanson constructs usethe standard timezone (GMT), notthe local time zone (whatever that might be). That is: Hymanson defaults to using GMT for all processing unlessspecifically told otherwise.

Hymanson 构造的所有与 TimeZone(java.util.Calendar 等)相关联的时间对象都使用标准时区 (GMT),而不是本地时区(无论是什么)。也就是说:除非另有特别说明,否则Hyman逊默认使用 GMT 进行所有处理。

In your case, it looks like the date is automatically being converted to GMT/UTC. Try to provide your local timezone explicitly to avoid UTC conversion [as mentioned in the question How come this time is off by 9 hours? (5 hours, 3 hours etc)on same page]:

在您的情况下,日期似乎会自动转换为 GMT/UTC。尝试明确提供您的本地时区以避免 UTC 转换 [如问题中提到的,这个时间为什么会延迟 9 小时?(5 小时、3 小时等)在同一页面]:

@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="dd/MM/yyyy", timezone="EST")

Secondly, I think you are using Date.toString()to print date. Notethat java Dateclass is timezone independent but its toString()method uses the system's defaulttimezone before printing.

其次,我认为您正在使用Date.toString()打印日期。请注意,javaDate类与时区无关,但其toString()方法在打印前使用系统的默认时区。

Here it looks like 24/07/2015 00:00 UTCis being converted to 23/07/2015 19:00 ESTby toString(). Both of these represent the same momentof time but in different timezones.

这看起来像24/07/2015 00:00 UTC被转换为23/07/2015 19:00 EST通过toString()。这两个代表同一时刻,但在不同的时区。

回答by MarkyMarksFunkyBunch

AimZ answer is what pointed me to this but I added these three lines to my application.properties file and achieved the same thing

AimZ 答案是我指出这一点的原因,但我将这三行添加到我的 application.properties 文件中并实现了相同的目的

spring.Hymanson.date-format=yyyy-MM-dd

spring.Hymanson.date-format=yyyy-MM-dd

spring.Hymanson.serialization.write-dates-as-timestamps:false

spring.Hymanson.serialization.write-dates-as-timestamps:false

spring.Hymanson.time-zone:EST

spring.Hymanson.time-zone:EST

回答by Chrispie

Had the same issue. Using postman to verify that the client is not the culprit. Seems like an issue with the timezone Hymanson is using vs the timezone of the system. Had to change the Hymanson config to compensate for dates

有同样的问题。使用邮递员验证客户端不是罪魁祸首。似乎Hyman逊使用的时区与系统的时区存在问题。必须更改 Hymanson 配置以补偿日期

@Configuration

public class HymansonConfig {

    @Bean
    @Primary
    public Hymanson2ObjectMapperBuilder Hymanson2ObjectMapperBuilder() {
        final Hymanson2ObjectMapperBuilder Hymanson2ObjectMapperBuilder = new Hymanson2ObjectMapperBuilder();
        Hymanson2ObjectMapperBuilder.timeZone(TimeZone.getDefault());
        Hymanson2ObjectMapperBuilder.serializationInclusion(JsonInclude.Include.NON_EMPTY);
        Hymanson2ObjectMapperBuilder.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        return Hymanson2ObjectMapperBuilder;
    }

}

回答by Lorena Pita

I had the same issue in JavaScriptwhen selecting dates from a datepicker. I formatted the fields using .toString()method but the function gave me a different date (I was messing a day as well). Like this:

从日期选择器中选择日期时,我在JavaScript 中遇到了同样的问题。我使用.toString()方法格式化字段,但该函数给了我一个不同的日期(我也搞砸了一天)。像这样:

var mydate = new Date('2020-04-03'); console.log(mydate.toString());

var mydate = new Date('2020-04-03'); console.log(mydate.toString());

//Thu Apr 02 2020 20:00:00 GMT-0400 (Eastern Daylight Time)

//Thu Apr 02 2020 20:00:00 GMT-0400 (Eastern Daylight Time)

I fixed it ussing .toUTCString()instead.

我改用.toUTCString()修复了它。

var mydate = new Date('2020-04-03');

console.log(mydate.toUTCString());

//Fri, 03 Apr 2020 00:00:00 GMT