我想从用户那里获取时间和时区并创建一个条目。使用下面的日历 API 来执行此操作,它适用于几个时区而不适用于几个时区
calendar.setTime(eventFormEntryBean.getStartDate());
TimeZone timeZone = TimeZone.getTimeZone("Europe/Amsterdam");
calendar.setTimeZone(timeZone);
工作时区(最后+xx:xx)
- 太平洋/帕劳 2019-11-27T20:51:09.000+09:00
- IST - 2019-11-20T22:00:00.000+05:30
- 欧洲/阿姆斯特丹 - 2019-11-28T12:49:24.000+01:00
- 美国/洛杉矶 - 2019-11-20T21:32:49.000-08:00
不工作时区:-
- 非洲/达喀尔 - 2019-11-21T05:30:45.000Z
- 伦敦(欧洲/伦敦)- 2019-11-21T12:08:42.000Z
对于上述伦敦和非洲/达喀尔时区,没有任何区分时区的标志,只是在末尾注明“.000Z”。是否需要设置任何属性才能获得完整时区? .000z 是什么意思?
最佳答案
如果您希望能够编写反射(reflect)偏移量和时区之间差异的代码,请离开 java.util
并切换到 java.time
(对于 Java 8 + 和 support library for Java 6 and 7 )。
然后你可以做这样的事情:
public static void main(String[] args) {
/*
* the base of this example is a date time with an offset of +01:00
* (which is present in several zones, not just in Europe/Amsterdam!)
*/
String datetime = "2019-11-28T12:49:24.000+01:00";
// parse it to an offset-aware object
OffsetDateTime plusOneHourOffsetDateTime = OffsetDateTime.parse(datetime);
// print it to be sure ;-)
System.out.println(plusOneHourOffsetDateTime
.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
// convert it to a zone-aware date time object by providing the zone
ZonedDateTime europeAmsterdamZonedDateTime = plusOneHourOffsetDateTime
.atZoneSameInstant(ZoneId.of("Europe/Amsterdam"));
// print it
System.out.println(europeAmsterdamZonedDateTime
.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
// then take the same instant but use a different time zone
ZonedDateTime utcZonedDateTime = plusOneHourOffsetDateTime
.atZoneSameInstant(ZoneId.of("UTC"));
// print that, it adds a Z (indicating an offset of 00:00) and the time zone
// that was specified
System.out.println(utcZonedDateTime.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
// take a totally different time zone and do it again
ZonedDateTime pacificPalauZonedDateTime = plusOneHourOffsetDateTime
.atZoneSameInstant(ZoneId.of("Pacific/Palau"));
// print that one, too
System.out.println(pacificPalauZonedDateTime
.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
}
输出这个
2019-11-28T12:49:24+01:00
2019-11-28T12:49:24+01:00[Europe/Amsterdam]
2019-11-28T11:49:24Z[UTC]
2019-11-28T20:49:24+09:00[Pacific/Palau]
编辑
The reason for the
DateTimeParseException
mentioned in your comment is the date-timeString
, because it doesn't have a zone or an offset, which makes it unparseable by the defaultDateTimeFormatter
used inOffsetDateTime.parse(String datetime)
.
如果您有一个包含日期和时间信息但没有区域或偏移量的 String
,您可以先将其解析为 LocalDateTime
并创建一个 ZonedDateTime
来自:
public static void main(String[] args) {
// date time String without zone or offset information
String dateTimeString = "2019-11-30T19:35:06";
// create a LocalDateTime from the String
LocalDateTime ldt = LocalDateTime.parse(dateTimeString);
// then create a ZonedDateTime from the LocalDateTime adding a zone
ZonedDateTime zdt = ldt.atZone(ZoneId.systemDefault()); // system default here
// and print it
System.out.println(zdt.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
}
关于java - java中的几个时区没有设置偏移量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58954574/