如何在 Java8 中将 UTC 转换为 EST java.util.date?
我的代码如下,可将 UTC 转换为 EST。但是,如何从下面的代码以及这种格式 (yyyy-MM-dd'T'HH:mm:ss) 将 ZonedDateTime 转换为 java.util.Date ?
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
String f = "yyyy-MM-dd'T'HH:mm:ss";
String timestamp = "2022-03-01T16:29:03"; //sample input
TemporalAccessor temporalAccessor = formatter.parse(timestamp);
LocalDateTime localDateTime = LocalDateTime.from(temporalAccessor);
ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, ZoneId.systemDefault());
Instant result = Instant.from(zonedDateTime);
ZonedDateTime nyTime = result.atZone(ZoneId.of("Canada/Eastern"));
DateTimeFormatter format = DateTimeFormatter.ofPattern(f);
System.out.println("Date EST : " + format.format(nyTime));
I have my code below which converts UTC to EST. But, how can I convert ZonedDateTime to java.util.Date from the below code along with this format (yyyy-MM-dd'T'HH:mm:ss) ?
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
String f = "yyyy-MM-dd'T'HH:mm:ss";
String timestamp = "2022-03-01T16:29:03"; //sample input
TemporalAccessor temporalAccessor = formatter.parse(timestamp);
LocalDateTime localDateTime = LocalDateTime.from(temporalAccessor);
ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, ZoneId.systemDefault());
Instant result = Instant.from(zonedDateTime);
ZonedDateTime nyTime = result.atZone(ZoneId.of("Canada/Eastern"));
DateTimeFormatter format = DateTimeFormatter.ofPattern(f);
System.out.println("Date EST : " + format.format(nyTime));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
避免遗留类
您评论过:
切勿使用
Date类。仅使用java.time类。 JDBC 4.2 及更高版本支持 java.time 与数据库交换日期时间值。
TIMESTAMP WITHOUT TIME ZONE
类型的列,请使用java.time.LocalDateTime
。TIMESTAMP WITH TIME ZONE
类型的列,请使用java.time.OffsetDateTime
。不一会儿
您输入的字符串:
… 命名错误。该值缺少时区或与 UTC 的偏移量指示符。所以它不能代表一个时刻。我们无法知道这是否是在日本东京、法国图卢兹或美国俄亥俄州托莱多的下午 4:30 左右——所有时间都相隔几个小时,截然不同。因此称其为“时间戳”是有误导性的。
将该输入字符串解析为
LocalDateTime
。无需指定格式模式。该字符串符合 ISO 8691 标准,在解析/生成文本时默认在 java.time 类中使用。此时
您的代码似乎很复杂。您为此日期和时间分配默认时区。但这似乎不太有道理。我猜测您的意图是让这个特定的日期与时间代表特定时区中的特定时刻。
显然你想要加拿大东部
Avoid legacy classes
You commented:
Never use either
Date
class. Use only java.time classes. JDBC 4.2 and later supports java.time for exchanging date-time values with a database.TIMESTAMP WITHOUT TIME ZONE
, usejava.time.LocalDateTime
.TIMESTAMP WITH TIME ZONE
, usejava.time.OffsetDateTime
.Not a moment
Your input string:
… is misnamed. That value lacks an indicator of time zone or offset-from-UTC. So it cannot represent a moment. We have no way of knowing if that is around 4:30 PM in Tokyo Japan, or in Toulouse France, or in Toledo Ohio US — all very different moments several hours apart. So calling this a “timestamp” is misleading.
Parse that input string as a
LocalDateTime
. No need to specify a formatting pattern. That string complies with ISO 8691, used by default in the java.time classses when parsing/generating text.Moment
Your code at this point seems convoluted. You assign a default time zone to this date-with-time. But that seems unlikely to make sense. I am guessing that your intention was for this specific date-with-time to represent a specific moment as seen in a specific time zone.
Apparently you want eastern Canada ???????? time zone. The proper name for that zone is
America/Toronto
.Assign that zone to produce a
ZonedDateTime
object.Now we have a moment, a specific point on the timeline.
JDBC
But for writing to a database column of a type akin to the SQL-standard
TIMESTAMP WITH TIME ZONE
, we need to useOffsetDateTime
.Later you commented that the input string is intended to represent a moment in UTC. So assign a
ZoneOffset
object, the constantUTC
, to get anOffsetDateTime
.Send to the database.
Retrieval.
Converting legacy ↔ modern
If you must use
java.util.Date
to interoperate with old code not yet updated to java.time, you can convert to and fro. Look to the newto…
andfrom…
methods added to the old classes.Going the other direction.
All of these topics have been addressed many times on Stack Overflow. Search to learn more.
一旦您的
ZonedDateTime
位于正确的时区,请将其转换为 Instant,然后使用方法 来自类 Date 的公共静态日期(即时)。但是,如果可以的话,请避免使用 Date 类,该类是过时的(双关语)包
java.time
以及ZonedDateTime
、Instant
等类和其他应该使用
Once you have
ZonedDateTime
in correct time zone convert it to Instant and then use method public static Date from(Instant instant) of class Date.However, if you can, refrain from usage of Date class which is outdated (pun intended) package
java.time
with classes likeZonedDateTime
,Instant
and others should be used