javascript日期到java.time.LocalDate [英] javascript date to java.time.LocalDate
问题描述
我正在尝试将json数据发布到Java中的Controller.
I'm trying to post json data to a Controller in Java.
这是我的控制者:
@ResponseBody
@RequestMapping(value="/{schoolId}", method=RequestMethod.POST)
public ClassGroupDTO addClassGroup(@RequestBody ClassGroupDTO classgroup, @PathVariable Integer schoolId) {
return partyService.addClassGroup(classgroup, schoolId);
}
这是ClassGroupDTO
This it the ClassGroupDTO
public class ClassGroupDTO extends PartyDTO {
private PartyDTO titular;
private SiteDTO site;
@JsonDeserialize(using = LocalDateDeserializer.class)
private LocalDate startDate;
@JsonDeserialize(using = LocalDateDeserializer.class)
private LocalDate endDate;
...
}
我正在使用Jackson 2.4.3.
I'm using Jackson 2.4.3.
给定字段startDate或endDate时,我无法发布数据. 我尝试了几种格式来发布. (我正在使用moment.js)
I'm not able to post data when the field startDate or endDate is given. I've tried several formats to post. (I'm using moment.js)
data.startDate = moment().toDate();
data.startDate = moment().toJSON();
data.startDate = moment().format("YYYY/MM/DD");
每次我都会收到一个错误的请求错误. 当我忽略startDate或endDate时,将发布数据并触发控制器.
Everytime I receive a Bad Request error. When I leave out startDate or endDate the data is posted and the controller is triggered.
如何将JavaScript日期反序列化为java.time.LocalDate?
How to deserialize javascript date to java.time.LocalDate?
推荐答案
我遇到了同样的问题,使用以下方法解决了该问题:
I got the same problem, solved it using:
var dateString = new Date().toISOString().substring(0,10);
或
var dateString = new Date().toISOString().split("T")[0];
转换为ISO字符串("2015-10-14T09:39:49.942Z"),然后仅保留前十个字符,即日期.
Convert to ISO string ("2015-10-14T09:39:49.942Z"), then keep only first ten characters ie, the date.
这篇关于javascript日期到java.time.LocalDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!