如何在 Java8 中将 UTC 转换为 EST java.util.date?

发布于 2025-01-13 05:32:13 字数 799 浏览 0 评论 0原文

我的代码如下,可将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瑶笙 2025-01-20 05:32:13

避免遗留类

您评论过:

我需要将其填充到数据库列中。所以我需要将 UTC 格式的字符串转换为 EST,然后将 ZonedDateTime 转换回这种格式的 java.util.Date (yyyy-MM-dd'T'HH:mm:ss)

切勿使用 Date类。仅使用java.time类。 JDBC 4.2 及更高版本支持 java.time 与数据库交换日期时间值。

  • 对于类似于 SQL 标准类型 TIMESTAMP WITHOUT TIME ZONE 类型的列,请使用 java.time.LocalDateTime
  • 对于类似于 SQL 标准类型 TIMESTAMP WITH TIME ZONE 类型的列,请使用 java.time.OffsetDateTime

不一会儿

您输入的字符串:

字符串时间戳 = "2022-03-01T16:29:03";

… 命名错误。该值缺少时区或与 UTC 的偏移量指示符。所以它不能代表一个时刻。我们无法知道这是否是在日本东京、法国图卢兹或美国俄亥俄州托莱多的下午 4:30 左右——所有时间都相隔几个小时,截然不同。因此称其为“时间戳”是有误导性的。

将该输入字符串解析为 LocalDateTime。无需指定格式模式。该字符串符合 ISO 8691 标准,在解析/生成文本时默认在 java.time 类中使用。

LocalDateTime ldt = LocalDateTime.parse( "2022-03-01T16:29:03" ) ;

此时

您的代码似乎很复杂。您为此日期和时间分配默认时区。但这似乎不太有道理。我猜测您的意图是让这个特定的日期与时间代表特定时区中的特定时刻。

显然你想要加拿大东部

Avoid legacy classes

You commented:

I need to populate this in DB column. So I need to convert a string which is in UTC to EST then converting ZonedDateTime back to java.util.Date in this format (yyyy-MM-dd'T'HH:mm:ss)

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.

  • For a column of a type akin to the SQL-standard type TIMESTAMP WITHOUT TIME ZONE, use java.time.LocalDateTime.
  • For a column of a type akin to the SQL-standard type TIMESTAMP WITH TIME ZONE, use java.time.OffsetDateTime.

Not a moment

Your input string:

String timestamp = "2022-03-01T16:29:03";

… 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.

LocalDateTime ldt = LocalDateTime.parse( "2022-03-01T16:29:03" ) ;

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.

ZoneId z = ZoneId.of( "America/Toronto" ) ;

Assign that zone to produce a ZonedDateTime object.

ZonedDateTime zdt = ldt.atZone( z ) ;

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 use OffsetDateTime.

OffsetDateTime odt = zdt.toOffsetDateTime() ;

Later you commented that the input string is intended to represent a moment in UTC. So assign a ZoneOffset object, the constant UTC, to get an OffsetDateTime.

OffsetDateTime odt = ldt.atOffset( ZoneOffset.UTC ) ;

Send to the database.

myPreparedStatement.setObject( … , odt ) ;

Retrieval.

OffsetDateTime odt = myResultSet.getObject( … , OffsetDateTime.class ) ;
ZonedDateTime zdt = odt.atZoneSameInstant( z ) ;

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 new to… and from… methods added to the old classes.

java.util.Date d = Date.from( odt.toInstant() ) ;
java.util.Date d = Date.from( zdt.toInstant() ) ;

Going the other direction.

Instant instant = myJavaUtilDate.toInstant() ;

All of these topics have been addressed many times on Stack Overflow. Search to learn more.

荒芜了季节 2025-01-20 05:32:13

一旦您的 ZonedDateTime 位于正确的时区,请将其转换为 Instant,然后使用方法 来自类 Date 的公共静态日期(即时)

Instant instant = Instant.from(zonedDateTime);
Date date = Date.from(instant);

但是,如果可以的话,请避免使用 Date 类,该类是过时的(双关语)包 java.time 以及 ZonedDateTimeInstant 等类
和其他应该使用

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.

Instant instant = Instant.from(zonedDateTime);
Date date = Date.from(instant);

However, if you can, refrain from usage of Date class which is outdated (pun intended) package java.time with classes like ZonedDateTime, Instant
and others should be used

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文