admin管理员组

文章数量:1433506

I have user date picker where a user selects the date and it then makes an AJAX call to a rest service implemented in Java. But the issue is it always returns one-day previous date object. Here is my implementation:

testDate = $("#date-select").val();
console.log(testDate)

Above console.log prints the correct date. 2018-04-22 But when it gets passed to rest service, it shows the wrong date:

@POST
@Path("checkDate/{testDate}")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response demoService(final DemoBean demoBean)
{
    System.out.println("Rest service got called:"+ demoBean.getDate());
    if(demoDateConfService.setDate(demoBean.getDate())
    {
        return Response.ok(new Result(true)).cacheControl(NO_CACHE).build();
    }
    return Response.ok(new Result(false)).cacheControl(NO_CACHE).build();
}

This is the result of the Rest Call:

Rest service got called:Sat Apr 21 20:00:00 EDT 2018

Not sure if it has anything to do with timezone. My laptop is running in EST timezone.

I have user date picker where a user selects the date and it then makes an AJAX call to a rest service implemented in Java. But the issue is it always returns one-day previous date object. Here is my implementation:

testDate = $("#date-select").val();
console.log(testDate)

Above console.log prints the correct date. 2018-04-22 But when it gets passed to rest service, it shows the wrong date:

@POST
@Path("checkDate/{testDate}")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response demoService(final DemoBean demoBean)
{
    System.out.println("Rest service got called:"+ demoBean.getDate());
    if(demoDateConfService.setDate(demoBean.getDate())
    {
        return Response.ok(new Result(true)).cacheControl(NO_CACHE).build();
    }
    return Response.ok(new Result(false)).cacheControl(NO_CACHE).build();
}

This is the result of the Rest Call:

Rest service got called:Sat Apr 21 20:00:00 EDT 2018

Not sure if it has anything to do with timezone. My laptop is running in EST timezone.

Share Improve this question asked Apr 20, 2018 at 20:36 user_devuser_dev 1,4314 gold badges23 silver badges49 bronze badges 1
  • 6 Apr 21 8pm EDT == Apr 22 midnight GMT, print your date in GMT timezone and you will get Apr 22 00:00:00 – hoaz Commented Apr 20, 2018 at 20:38
Add a ment  | 

1 Answer 1

Reset to default 8

java.util.Date is the most confusing class in Java Core. I would not remend to pass dates using java.util.Date, as it is intended to store both date and time.

As I pointed out in ments Apr 21 8pm EDT == Apr 22 midnight GMT. Print your date in GMT timezone and you will get Apr 22 00:00:00:

SimpleDateFormat sdf = new SimpleDateFormat("MMM d, yyyy hh:mm:ss a z");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println("Rest service got called:"+ sdf.format(demoBean.getDate()));

But I would rather remend to send date as string or use java.time.LocalDate.

本文标签: javascriptjava Date object is always one day lessStack Overflow