我可以从不同区域的 LocalDateTime 获得正确的日期时间吗?
Can I get correct datetime from LocalDateTime in defferent zone?
我使用 LocalDateTime.now()
在数据库中保存了当前日期时间,我看到它被保存为键值的 Map
,在地图中我看到时间、月、年、秒的键, 纳米——等等。但我看不到有关区域的信息。因此,如果在不同区域检索相同的时间日期,比如美国(从印度保存的数据),那么该怎么做?
Date date = new Date();
ZonedDateTime zonedDateTime = date.toInstant().atZone(ZoneId.of("US/Eastern"));
有不同的时区,根据它们的值你可以有日期时间。
LocalDateTime 与地点或时区无关
引用 ,查看关于 LocalDateTime 的 900+ upvotes 的答案:
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.
引用 的更多内容,关于 java-time 类型用法:
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.
以下是指定时区的推荐类型之一的示例“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 的信息:
,查看 900+ 赞的答案。
您可以在此处阅读有关 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
如overview of modern date-time classes in Java所示,有time-zone信息的class有ZonedDateTime
、OffsetDateTime
、OffsetTime
等。 class、LocalDateTime
没有time-zone信息。
如前所述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.
下面给出了使用 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();
了解有关现代 date-time API 的更多信息
我使用 LocalDateTime.now()
在数据库中保存了当前日期时间,我看到它被保存为键值的 Map
,在地图中我看到时间、月、年、秒的键, 纳米——等等。但我看不到有关区域的信息。因此,如果在不同区域检索相同的时间日期,比如美国(从印度保存的数据),那么该怎么做?
Date date = new Date();
ZonedDateTime zonedDateTime = date.toInstant().atZone(ZoneId.of("US/Eastern"));
有不同的时区,根据它们的值你可以有日期时间。
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.
引用
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.
以下是指定时区的推荐类型之一的示例“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():
查看可用区域 IDZoneId.getAvailableZoneIds().stream().forEach(System.out::println);
了解更多关于 java.time 的信息:
您可以在此处阅读有关 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
如overview of modern date-time classes in Java所示,有time-zone信息的class有ZonedDateTime
、OffsetDateTime
、OffsetTime
等。 class、LocalDateTime
没有time-zone信息。
如前所述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
withLocalTime
. 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 aZonedDateTime
or anOffsetDateTime
, as discussed in Time Zone and Offset Classes.
下面给出了使用 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();
了解有关现代 date-time API 的更多信息