Spring @DateTimeFormat 在将 @PathVariable 转换为 Date 时正在更改时区
Spring @DateTimeFormat is changing timezone while converting @PathVariable to Date
当使用 @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.util.Date即时时间被转换回东部时区,因此您可以看到该值。如果你用的是Java8,LocalDate应该可以帮你解决问题。
@PathVariable @DateTimeFormat(pattern = "dd-MM-yyyy") LocalDate date
下面post一些选项给你整齐
Nhu Vy answer 指出了正确的方向;但是我没有添加时区信息,而是不得不删除它。
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
已将其从配置中删除并保留默认时区。这对我有用。
我遇到了同样的问题,我只在我的 Serializer 文件中添加了 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');
}
}
此代码提取自
希望对您有所帮助。
当使用 @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.util.Date即时时间被转换回东部时区,因此您可以看到该值。如果你用的是Java8,LocalDate应该可以帮你解决问题。
@PathVariable @DateTimeFormat(pattern = "dd-MM-yyyy") LocalDate date
下面post一些选项给你整齐
Nhu Vy answer 指出了正确的方向;但是我没有添加时区信息,而是不得不删除它。
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
已将其从配置中删除并保留默认时区。这对我有用。
我遇到了同样的问题,我只在我的 Serializer 文件中添加了 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');
}
}
此代码提取自
希望对您有所帮助。