Vin*_*tto 3 java timezone datetime date
考虑到特定时区 tz,我需要将LocalDateTimeUTC 日期转换为另一个LocalDateTime变量。
在我的研究,我发现很多的解决方案,但它们都转换LocalDateTime为另一种类型,喜欢ZonedDateTime。
我需要这样的东西,但LocalDateTime不会与ZoneId:
LocalDateTime output = input.getInitDate().of(ZoneId.of(tz))
考虑 -3 时区:
input: 2019-12-03T18:24:07
output: 2019-12-03T15:24:07
Run Code Online (Sandbox Code Playgroud)
您需要首先将其转换为ZonedDateTime,更改时区,然后从中提取LocalDateTime:
ZoneId from = ...;
ZoneId to = ...;
LocalDateTime input = ...;
LocalDateTime output = input.atZone(from).withZoneSameInstant(to).toLocalDateTime();
Run Code Online (Sandbox Code Playgroud)
你的问题没有意义。
您需要了解LocalDateTime它只包含日期和时间。该类故意缺少时区或UTC 偏移量的任何概念。所以LocalDateTime不会不表示一个时刻。名称可以是误导,因为一个LocalDateTime是没有任何特定地区。
如果您想以 UTC 时间跟踪某个时刻,请使用Instant.
Instant instant = Instant.now() ; // Capture the current moment in UTC. Always in UTC, by definition.
Run Code Online (Sandbox Code Playgroud)
如果要跟踪特定地区(时区)的人们使用的挂钟时间中看到的某个时刻,请使用ZonedDateTime.
ZoneId z = ZoneId.of( "America/Montevideo" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ;
Run Code Online (Sandbox Code Playgroud)
UTC 日期的 LocalDateTime 转换
你的意思是在本初子午线看到的你有特定的日期和时间吗?或者如冰岛所见,使用UTC作为时区?
LocalDate ld = LocalDate.of( 2020 , Month.JANUARY , 23 ) ;
LocalTime lt = LocalTime.of( 15 , 0 ) ; // 3 PM.
LocalDateTime ldt = LocalDateTime.of( ld , lt ) ;
Run Code Online (Sandbox Code Playgroud)
此ldt对象的意思是“今年 1 月 23 日下午 3 点”某处或任何地方。但该LocalDateTime对象不未下午3时在一个特定的地方。我们不知道这里的意图是日本东京下午 3 点还是法国图卢兹下午 3 点或美国俄亥俄州托莱多下午 3 点。那将是三个不同的时刻,相隔几个小时。ALocalDateTime不代表它们中的任何一个或全部,无论您想以哪种方式查看它,但不代表它们中的任何一个。
要根据日期和时间确定时刻,您需要时区或 UTC 偏移量的上下文。换句话说,第三条信息,除了日期和时间之外,我们还需要“如法国巴黎所见”或“如南极洲帕尔默站所见”。在这样的上下文中,我们分别得到ZonedDateTime或OffsetDateTime。
ZoneId z = ZoneId.of( "Antarctica/Palmer" ) ;
ZonedDateTime zdt = ZonedDateTime.of( ldt , z ) ; // Giving context of time zone to the date-with-time `LocalDateTime` object. Determines a moment.
Run Code Online (Sandbox Code Playgroud)
要查看 UTC 中的同一时刻(零时分秒的偏移量),请提取Instant.
Instant instant = zdt.toInstant() ; // Adjust from time zone to UTC.
Run Code Online (Sandbox Code Playgroud)
考虑 -3 时区:
不,-3是不是一个时区,它的偏移量。
的-3缩写-03:00。实际上,我建议您避免缩写偏移量,因为某些库需要完整的小时数和分钟数,包括冒号字符,并包括单位数小时或分钟的前导零。
该-3或-03:00手段简单“的背后UTC3小时”。
时区要多得多。时区是特定地区人民使用的偏移量的过去、现在和未来变化的历史记录。时区的名称格式Continent/Region为Africa/Tunis或Europe/Paris。
请参阅维基百科上的时区列表。按偏移列排序。请注意如何各地三十余个时区可能今天分享的偏移量-03:00,如America/Montevideo,Atlantic/Stanley和Antarctica/Palmer。
总是更喜欢时区而不是仅仅偏移。在进行日期时间数学运算和加/减时间跨度时,结果可能因时区而异。除了-03:00过去和未来的其他日期,时区可能使用不同的偏移量。
| 归档时间: |
|
| 查看次数: |
348 次 |
| 最近记录: |