英文:
Can I get correct datetime from LocalDateTime in defferent zone?
问题
我使用 LocalDateTime.now()
将当前日期时间保存在数据库中,我发现它保存为键值 Map
,在该映射中,我看到了时间、月份、年份、秒数、纳秒等的键。但是我没有看到关于时区的任何信息。因此,如果在不同的时区(例如从印度保存的数据)中检索相同的时间日期,该如何操作?
英文:
I saved current date time in database using LocalDateTime.now()
, i see that it is saved as Map
of key-value, In the map i see key for time, month , year, second , nano --etc. But i see nowhere information regarding zone. So if retrieve same time date in different zone say USA (data saved from India) then how to do it?
答案1
得分: 3
LocalDateTime不与地区或时区绑定
引用关于java.time的详细描述,查看获得1500多个赞的回答,关于LocalDateTime:
> 它们不与任何地区或时区绑定。它们不与时间线相关联。只有在将它们应用于特定地区以找到时间线上的一个点时,它们才有真正的意义。
从关于java.time的详细描述中引用更多关于java-time类型的用法:
> 所以对于业务应用程序来说,“Local”类型通常不经常使用,因为它们只代表可能的日期或时间的一般概念,而不是时间线上的特定时刻。业务应用程序往往关心发票到达的确切时刻,产品发运的时间,员工的入职时间或出租车离开车库的时间。因此,业务应用程序开发人员最常用Instant和ZonedDateTime类。
这里有一个使用推荐的时区“America/Los_Angeles”的示例:
ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.of("America/Los_Angeles"));
这是另一种做相同操作的变体:
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(Instant.now(), ZoneId.of("America/Los_Angeles"));
还有另一种做相同操作的变体:
ZonedDateTime zonedDateTime = ZonedDateTime.now().withZoneSameInstant(ZoneId.of("America/Los_Angeles"));
您可以使用**ZoneId.getAvailableZoneIds()**查看可用的时区ID:
ZoneId.getAvailableZoneIds().stream().forEach(System.out::println);
在以下链接中了解更多关于java.time的信息:
关于java.time的详细描述,查看获得1500多个赞的回答。
您可以在这里阅读有关ZonId和ZoneOffset的更多信息:
https://docs.oracle.com/javase/10/docs/api/java/time/ZoneId.html
https://docs.oracle.com/javase/10/docs/api/java/time/ZoneOffset.html
英文:
LocalDateTime is not tied to a locality or time zone
Quoting extensive description of java.time, see answer with 1500+ upvotes, about LocalDateTime:
> They are not tied to any one locality or time zone. They are not tied to the timeline. They have no real meaning until you apply them to a locality to find a point on the timeline.
Quoting more from extensive description of java.time, about java-time type usage:
> So for business apps, the "Local" types are not often used as they represent just the general idea of a possible date or time not a specific moment on the timeline. Business apps tend to care about the exact moment an invoice arrived, a product shipped for transport, an employee was hired, or the taxi left the garage. So business app developers use Instant and ZonedDateTime classes most commonly.
Here is an example with one of the recommended types for specifying time zone "America/Los_Angeles":
ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.of("America/Los_Angeles"));
Here is another variation doing the same thing:
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(Instant.now(), ZoneId.of("America/Los_Angeles"));
And another variation doing the same thing:
ZonedDateTime zonedDateTime = ZonedDateTime.now().withZoneSameInstant(ZoneId.of("America/Los_Angeles"));
You can see Available Zone Ids by using ZoneId.getAvailableZoneIds():
ZoneId.getAvailableZoneIds().stream().forEach(System.out::println);
Learn more about java.time at:
extensive description of java.time, see answer with 1500+ upvotes.
You can read more about ZonId and ZoneOffset here:
https://docs.oracle.com/javase/10/docs/api/java/time/ZoneId.html
https://docs.oracle.com/javase/10/docs/api/java/time/ZoneOffset.html
答案2
得分: 2
如在Java中现代日期时间类的概述中所示,具有时区信息的类包括ZonedDateTime
、OffsetDateTime
、OffsetTime
等。而LocalDateTime
类则不包含时区信息。
根据此处提到的内容:
处理日期和时间(不包含时区)的类是LocalDateTime,它是日期时间API的核心类之一。此类用于表示日期(月-日-年)以及时间(时-分-秒-纳秒),实际上是
LocalDate
与LocalTime
的组合。此类可用于表示特定事件,例如2013年8月17日下午1:10开始的路易威登杯决赛的第一场比赛。注意,这表示当地时间下的下午1:10。若要包含时区信息,必须使用ZonedDateTime
或OffsetDateTime
,如时区和偏移量类中所述。
以下是使用OffsetDateTime
的示例代码:
OffsetDateTime odt = OffsetDateTime.now(ZoneOffset.UTC); // 根据需要更改ZoneOffset
PreparedStatement st = conn.prepareStatement("INSERT INTO mytable (columnfoo) VALUES (?)");
st.setObject(1, odt);
st.executeUpdate();
st.close();
了解更多关于现代日期时间API的信息,访问**教程:日期时间**。
英文:
As shown in the overview of modern date-time classes in Java, the classes which have time-zone information are ZonedDateTime
, OffsetDateTime
, OffsetTime
etc. The class, LocalDateTime
does not have time-zone information.
As mentioned here,
> The class that handles both date and time, without a time zone, is
> LocalDateTime, one of the core classes of the Date-Time API. This
> class is used to represent date (month-day-year) together with time
> (hour-minute-second-nanosecond) and is, in effect, a combination of
> LocalDate
with LocalTime
. This class can be used to represent a
> specific event, such as the first race for the Louis Vuitton Cup
> Finals in the America's Cup Challenger Series, which began at 1:10
> p.m. on August 17, 2013. Note that this means 1:10 p.m. in local time.
> To include a time zone, you must use a ZonedDateTime
or an
> OffsetDateTime
, as discussed in Time Zone and Offset Classes.
Given below is an example code for working with OffsetDateTime
:
OffsetDateTime odt = OffsetDateTime.now(ZoneOffset.UTC);// Change ZoneOffset as applicable
PreparedStatement st = conn.prepareStatement("INSERT INTO mytable (columnfoo) VALUES (?)");
st.setObject(1, odt);
st.executeUpdate();
st.close();
Learn more about the modern date-time API at Trail: Date Time.
答案3
得分: 0
Date date = new Date();
ZonedDateTime zonedDateTime = date.toInstant().atZone(ZoneId.of("US/Eastern"));
英文:
Date date = new Date();
ZonedDateTime zonedDateTime = date.toInstant().atZone(ZoneId.of("US/Eastern"));
There are different Time zones, on the basis of their values you can have datetime.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论