如何在特定时区获得一个特定时间(如12:00:00.000)的日期实例?

huangapple go评论65阅读模式
英文:

How to get a Date instance with time of a day at a certain time (like 12:00:00.000) in a specific time zone?

问题

例如,现在是越南的2020-03-16 11:23:23.121,但我的程序在美国运行,如何获得一个在越南时间上为2020-03-16 12:00:00.000的日期实例,也就是说,保持年份、月份和日期不变,但将小时设置为12,将分钟、秒钟和纳秒设置为0,LocalDateTime能起到作用吗?

英文:

for example, now it is 2020-03-16 11:23:23.121 in Vietnam, but my program is running in the USA, how to get a Date instance which is 2020-03-16 12:00:00.000 in Vietnam, which mean, I keep the year, month, day as the same, but set hour as 12, minute, second and nanosecond as 0, can LocalDateTime play a role?

答案1

得分: 3

ZonedDateTime zdt = ZonedDateTime.of(2020, 3, 16, 12, 0, 0, 0, ZoneId.of("Asia/Ho_Chi_Minh"));

英文:
ZonedDateTime zdt = ZonedDateTime.of(2020, 3, 16, 12, 0, 0, 0, ZoneId.of("Asia/Ho_Chi_Minh"));

答案2

得分: 2

ZonedDateTime

从Java 8 开始,你可以使用ZonedDateTime来获取任意时区的日期和时间

ZonedDateTime dateTime = ZonedDateTime.now(ZoneId.of("Asia/Ho_Chi_Minh"));

然后,你可以使用with方法将时间修改为 12:00:00。将一天中的时间作为LocalTime对象传递给LocalTime.of方法。在新的LocalTime对象中,秒和纳秒默认为零,所以不需要将这些参数传递给工厂方法。

dateTime.with(LocalTime.of(12, 0));  //2020-03-16T12:00+07:00[Asia/Ho_Chi_Minh]

Java的util.Date不会存储任何时区信息,它只表示特定的时间点(仅为UTC),精确到毫秒。我建议避免使用旧的util.Date

英文:

ZonedDateTime

From java-8 you can use ZonedDateTime to get the date time from any zone

ZonedDateTime dateTime = ZonedDateTime.now(ZoneId.of("Asia/Ho_Chi_Minh"))

And the you can modify the time to 12:00:00 using with method. Pass the time of day as a LocalTime object obtained by calling LocalTime.of. In the new LocalTime object, the second and the nanosecond default to zero, so no need to pass those arguments to the factory method.

dateTime.with( LocalTime.of( 12 , 0 ) )  //2020-03-16T12:00+07:00[Asia/Ho_Chi_Minh]

Java util Date will not store any time zone information and it just represents a specific instant in time (which is only UTC), with millisecond precision. I will suggest to avoid using legacy util.Date

答案3

得分: 0

不要,在这里不要使用 LocalDateTime

> LocalDateTime 能发挥作用吗?

LocalDateTime 无法表示一个时刻,因为它缺乏时区或者与UTC的偏移量。所以在你的问题中使用这个类是完全错误的。

为了表示一个时刻,在时间线上的特定点,使用:

  • Instant(始终使用UTC)
  • OffsetDateTime(携带UTC偏移量,小时-分钟-秒的数量)
  • ZonedDateTime(携带指定的时区,以 Continent/Region 格式命名)

请查看正确的 Deadpool 给出的回答,展示了如何正确使用 ZonedDateTime 来解决你的问题。

欲了解更多信息,参见 Instant 和 LocalDateTime 之间的区别是什么?

英文:

No, do not use LocalDateTime here

> can LocalDateTime play a role?

LocalDateTime cannot represent a moment, as it lacks the context of a time zone or offset-from-UTC. So that is exactly the wrong classs to use here on your Question.

To represent a moment, a specific point on the timeline, use:

  • Instant (always in UTC)
  • OffsetDateTime (carries an offset-from-UTC, a number of hours-minutes-seconds)
  • ZonedDateTime (carries an assigned time zone, named in Continent/Region)

See the correct Answer by Deadpool showing the proper use of ZonedDateTime to solve your problem.

For more info, see What's the difference between Instant and LocalDateTime?

huangapple
  • 本文由 发表于 2020年3月16日 11:26:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/60699980.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定