英文:
Convert LocalDateTime In any from timezone to any time zone in Java
问题
- 假设我的
LocalDateTime
在印度标准时间(IST),即(2020-08-14T15:41) - 需要将这个IST的
LocalDateTime
转换到任何时区,比如从IST到UTC
所需输出:2020-08-14T10:11 //表示上午10:11
星期五,2020年8月14日
如果来自任何时区的LocalDateTime
,则将相应地获得UTC时间,与任何时区时间相关。
有一个通用解决方案,可以将任何时区的LocalDateTime转换为任何时区:
LocalDateTime localDateTimeOfFromTimeZone = LocalDateTime.now();//这会获取您的默认应用程序LocalDateTime
System.out.println("From LocalDateTime : " + localDateTimeOfFromTimeZone);
ZonedDateTime fromTimeZonedDateTime = localDateTimeOfFromTimeZone.atZone(fromTimeZoneId);
LocalDateTime localDateTimeOfToTimeZone = LocalDateTime.from(fromTimeZonedDateTime.withZoneSameInstant(toTimeZoneId));
System.out.println("Converted To LocalDateTime :" + localDateTimeOfToTimeZone);
或者
LocalDateTime newDateTime = oldDateTime.atZone(oldZone).withZoneSameInstant(newZone).toLocalDateTime();
英文:
- Suppose My
LocalDateTime
in IST i.e. (2020-08-14T15:41) - Need to convert this IST
LocalDateTime
to any time zone let's say IST to UTC
Required Output : 2020-08-14T10:11 //means 10:11 am
Friday, 14 August 2020
If any LocalDateTime
from any zone then will get UTC time accordingly UTC time with respect to any time zone time.
Got a general solution to convert LocalDateTime from any time zone to any time zone :
LocalDateTime localDateTimeOfFromTimeZone = LocalDateTime.now();//it's takes your default application LocalDateTime
System.out.println("From LocalDateTime : "+localDateTimeOfFromTimeZone);
ZonedDateTime fromTimeZonedDateTime = localDateTimeOfFromTimeZone.atZone(fromTimeZoneId);
LocalDateTime localDateTimeOfToTimeZone = LocalDateTime.from(fromTimeZonedDateTime.withZoneSameInstant(toTimeZoneId));
System.out.println("Converted To LocalDateTime :"+localDateTimeOfToTimeZone);
Or
LocalDateTime newDateTime = oldDateTime.atZone(oldZone).withZoneSameInstant(newZone).toLocalDateTime();
答案1
得分: 0
Sure, here's the translation of the code snippet:
我建议不要使用 `LocalDateTime` 来表示带有时区的日期和时间。`ZonedDateTime` 是处理这种情况的类。`LocalDateTime` 正好表示不带时区的日期和时间。
ZoneId fromTimeZoneId = ZoneId.of("Asia/Kolkata");
ZoneId toTimeZoneId = ZoneId.of("Etc/UTC");
ZonedDateTime dateTimeOfFromTimeZone = ZonedDateTime.now(fromTimeZoneId);
System.out.println("来自 ZonedDateTime:" + dateTimeOfFromTimeZone);
ZonedDateTime dateTimeOfToTimeZone
= dateTimeOfFromTimeZone.withZoneSameInstant(toTimeZoneId);
System.out.println("转换为 ZonedDateTime:" + dateTimeOfToTimeZone);
输出结果(我刚刚运行代码时的输出):
来自 ZonedDateTime: 2020-08-15T00:13:19.519646+05:30[Asia/Kolkata] 转换为 ZonedDateTime: 2020-08-14T18:43:19.519646Z[Etc/UTC]
使用 ZonedDateTime
而不是 LocalDateTime
,Java 会跟踪每个日期和时间对象的时区,这使得在转换中更难出现错误。这是一个难以高估的优势。
英文:
I recommend against using LocalDateTime
for at date and time in a time zone. ZonedDateTime
is the class for that. A LocalDateTime
is exactly a date and time without time zone.
ZoneId fromTimeZoneId = ZoneId.of("Asia/Kolkata");
ZoneId toTimeZoneId = ZoneId.of("Etc/UTC");
ZonedDateTime dateTimeOfFromTimeZone = ZonedDateTime.now(fromTimeZoneId);
System.out.println("From ZonedDateTime: " + dateTimeOfFromTimeZone);
ZonedDateTime dateTimeOfToTimeZone
= dateTimeOfFromTimeZone.withZoneSameInstant(toTimeZoneId);
System.out.println("Converted To ZonedDateTime: " + dateTimeOfToTimeZone);
Output when I ran the code just now:
> From ZonedDateTime: 2020-08-15T00:13:19.519646+05:30[Asia/Kolkata]
> Converted To ZonedDateTime: 2020-08-14T18:43:19.519646Z[Etc/UTC]
When using ZonedDateTime
rather than LocalDateTime
, Java keeps track of the time zone of each date and time object, which makes it considerably harder to make mistakes in conversions. An advantage that it is hard to overestimate.
专注分享java语言的经验与见解,让所有开发者获益!
评论