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
1 Answer
Reset to default 8java.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
版权声明:本文标题:javascript - java Date object is always one day less - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745602027a2665622.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论