对于不同的时区,没有时间转换日期 [英] Converting date without time for different timezone
问题描述
我将日期存储在 UTC 中并显示在用户时区中。但是,当我试图只在没有时间的情况下存储它时会误导到不同的日期。
I store the date in UTC long and displayed in user timezone. But when I try to store only days without time it misleading to different dates.
例如:2016年5月6日(2016年5月6日)的计划活动。此日期对于没有时区的所有地区都是唯一的。如果GMT + 5:30时区的用户试图在 05/06/2016 上添加活动,那么ISO-8601格式为 2016-05-05T16:00:00.000Z 和毫秒 1462464000000 。
Eg: Scheduling event on 05/06/2016 (06 May 2016). This date is unique for all regions without timezone. If user from GMT+5:30 timezone trying to add a event on 05/06/2016 then it ISO-8601 format is 2016-05-05T16:00:00.000Z and milliseconds is 1462464000000.
然后来自GMT时区的用户尝试查看此事件。日期 05/05/2016 而不是 05/06/2016 。
Then user from GMT timezone try to view this event. The date will be 05/05/2016 instead of 05/06/2016.
有什么办法吗?转换日期没有任何时区。
Is there any way to convert date without any timezone.
推荐答案
Java 8为您的问题提供了解决方案。如果您可以使用Java 8,请使用 java.time.LocalDate ,仅代表没有时间的日期。存储 long 值toEpochDay--rel =nofollow> toEpochDay 方法。
示例代码如下:
Java 8 provides the solution for your problem. If you can use Java 8, use java.time.LocalDate which represents only the date without the time. Store the long
value returned by toEpochDay method.
A sample code is given below:
LocalDate date = LocalDate.of(2016, 5, 4);
// Store this long value
long noOfDays = date.toEpochDay(); // No of days from 1970-01-01
LocalDate newDate = LocalDate.ofEpochDay(noOfDays);
System.out.println(newDate); // 2016-05-04
这篇关于对于不同的时区,没有时间转换日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!