Java 在引导数据采集器和LocalDate之间切换一天
我有一个表单,其中我使用一个字段Java 在引导数据采集器和LocalDate之间切换一天,java,json,jackson,date-parsing,bootstrap-timepicker,Java,Json,Jackson,Date Parsing,Bootstrap Timepicker,我有一个表单,其中我使用一个字段LocalDate,该字段从通过Jackson映射的表单接收值。 而JSON是: Tue Jun 27 2017 00:00:00 GMT+0200 (ora legale Europa occidentale) 我的LocalDate变量是2017-06-26 这是我的pojo: public class BookingForm { private Integer building; private Integer city; p
LocalDate
,该字段从通过Jackson
映射的表单接收值。
而JSON是:
Tue Jun 27 2017 00:00:00 GMT+0200 (ora legale Europa occidentale)
我的LocalDate
变量是2017-06-26
这是我的pojo:
public class BookingForm {
private Integer building;
private Integer city;
private LocalDate date;
private String description;
private LocalTime endTime;
private List<Integer> externalUsers;
private List<Integer> internalUsers;
private String name;
private Room room;
private LocalTime startTime;
private List<ExternalUser> newExternalUsers;
//get and set method
可以在这里进行配置吗?我认为您的Jackson设置为使用GMT,您的预订表单可能使用了其他类似BST的东西,这是造成此问题的原因我认为您的Jackson设置为使用GMT,您的预订表单可能使用了其他类似BST的东西,这是导致此问题的原因您需要添加
JavaTimeModule
反序列化程序集,此反序列化程序必须具有DateTimeFormatter
(以解析输入)
对于此测试,我创建了一个示例类:
public class JsonLocalDate {
private LocalDate date;
// getter/setter
}
然后:
String json = "{ \"date\": \"Tue Jun 27 2017 00:00:00 GMT+0200\" }";
ObjectMapper mapper = new ObjectMapper();
JavaTimeModule module = new JavaTimeModule();
// create formatter for the pattern
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
// append pattern
.appendPattern("EEE MMM dd yyyy HH:mm:ss 'GMT'Z")
// create formatter - use English Locale to parse weekday and month name
.toFormatter(Locale.ENGLISH);
// add the LocalDateDeserializer with the custom formatter
module.addDeserializer(LocalDate.class, new LocalDateDeserializer(formatter));
mapper.registerModule(module);
JsonLocalDate value = mapper.readValue(json, JsonLocalDate.class);
System.out.println(value.getDate());
输出为:
2017-06-27
您需要添加带有反序列化器集的
JavaTimeModule
,并且该反序列化器必须具有DateTimeFormatter
(以解析输入)
对于此测试,我创建了一个示例类:
public class JsonLocalDate {
private LocalDate date;
// getter/setter
}
然后:
String json = "{ \"date\": \"Tue Jun 27 2017 00:00:00 GMT+0200\" }";
ObjectMapper mapper = new ObjectMapper();
JavaTimeModule module = new JavaTimeModule();
// create formatter for the pattern
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
// append pattern
.appendPattern("EEE MMM dd yyyy HH:mm:ss 'GMT'Z")
// create formatter - use English Locale to parse weekday and month name
.toFormatter(Locale.ENGLISH);
// add the LocalDateDeserializer with the custom formatter
module.addDeserializer(LocalDate.class, new LocalDateDeserializer(formatter));
mapper.registerModule(module);
JsonLocalDate value = mapper.readValue(json, JsonLocalDate.class);
System.out.println(value.getDate());
输出为:
2017-06-27
虽然这可能回答了这个问题,但提供一个工作代码示例比仅仅指出问题要好得多。您可以在页面中找到更多提示。虽然这可能会回答问题,但提供一个有效的代码示例比仅仅指出问题要好得多。您可以在页面中找到更多提示。您好,我用我的spring配置进行了更新,您认为可以通过该方法进行设置吗?我不知道如何使用
FindAndRegisterModule
进行自定义配置。可能您需要单独设置。您好,我用我的spring配置进行了更新,您认为可以通过该方法进行设置吗?我不知道如何使用findAndRegisterModules
进行自定义配置。也许你得把它分开设置。