最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - java Date object is always one day less - Stack Overflow

matteradmin7PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far