格式化DateTime时如何应用时区? [英] How to apply timezone when formatting DateTime?
本文介绍了格式化DateTime时如何应用时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个日期时间作为 2011-01-11 01:51:10
,时区为 America / Los_Angeles
I have a datetime as 2011-01-11 01:51:10
and timezone as America/Los_Angeles
我想获得此值的本地化日期时间。这就是我所做的。
I want to get a localised date time for this value. This is what I do
val formatter1: DateTimeFormatter = DateTimeFormatter.ofPattern("y-M-d H:m:s");
val m1: LocalDateTime = LocalDateTime.parse("2011-01-11 01:51:10", formatter1);
println("DateTime: " + m1.atZone(ZoneId.of("America/Los_Angeles")))
我获得的值是
DateTime: 2011-01-11T01:51:10-08:00[America/Los_Angeles]
如何将其转换为本地化的datetime与 -08:00
偏移应用于它,没有 [America / Los_Angeles]
?
How do I convert it into localized datetime with -08:00
offset applied to it and no [America/Los_Angeles]
?
推荐答案
您首先必须指定您解析的时间在哪个时区,然后指定另一个转换为。
You first have to specify which timezone that the time which you have parsed is in. Then specify an other one to convert into.
DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("y-M-d H:m:s");
LocalDateTime m1 = LocalDateTime.parse("2011-01-11 01:51:10", formatter1);
ZonedDateTime z1 = m1.atZone(ZoneId.of("UTC"));
ZonedDateTime z2 = z1.withZoneSameInstant(ZoneId.of("America/Los_Angeles"));
System.out.println(z2.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
这篇关于格式化DateTime时如何应用时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文