最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

json - javascript date to java.time.LocalDate - Stack Overflow

programmeradmin1浏览0评论

I'm trying to post json data to a Controller in Java.

This is my controller:

@ResponseBody
    @RequestMapping(value="/{schoolId}", method=RequestMethod.POST)
    public ClassGroupDTO addClassGroup(@RequestBody ClassGroupDTO classgroup, @PathVariable Integer schoolId) {
        return partyService.addClassGroup(classgroup, schoolId);
    }

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;
...
}

I'm using Jackson 2.4.3.

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");

Everytime I receive a Bad Request error. When I leave out startDate or endDate the data is posted and the controller is triggered.

How to deserialize javascript date to java.time.LocalDate?

I'm trying to post json data to a Controller in Java.

This is my controller:

@ResponseBody
    @RequestMapping(value="/{schoolId}", method=RequestMethod.POST)
    public ClassGroupDTO addClassGroup(@RequestBody ClassGroupDTO classgroup, @PathVariable Integer schoolId) {
        return partyService.addClassGroup(classgroup, schoolId);
    }

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;
...
}

I'm using Jackson 2.4.3.

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");

Everytime I receive a Bad Request error. When I leave out startDate or endDate the data is posted and the controller is triggered.

How to deserialize javascript date to java.time.LocalDate?

Share Improve this question asked Oct 7, 2014 at 10:25 BakGatBakGat 6812 gold badges6 silver badges19 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 14

I got the same problem, solved it using:

var dateString = new Date().toISOString().substring(0,10);

or

var dateString = new Date().toISOString().split("T")[0];

Convert to ISO string ("2015-10-14T09:39:49.942Z"), then keep only first ten characters ie, the date.

Since you are using moment there is a simple way to achieve this :

let startDate = moment().toISOString();

This will convert to valid LocalDateTime format

发布评论

评论列表(0)

  1. 暂无评论