Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将LocalDateTime转换为GregorianCalendar?_Java_Java Time - Fatal编程技术网

Java 如何将LocalDateTime转换为GregorianCalendar?

Java 如何将LocalDateTime转换为GregorianCalendar?,java,java-time,Java,Java Time,如果无法解析简单的DateTime,为什么可以将LocalDateTime对象传递到ZonedDateTime.from()方法中,如下所示 @Test public void test() throws Exception { GregorianCalendar.from(ZonedDateTime.from(LocalDateTime.parse("2016-08-31T23:00:59"))); } 结果: java.time.DateTimeException: Unable

如果无法解析简单的
DateTime
,为什么可以将
LocalDateTime
对象传递到
ZonedDateTime.from()
方法中,如下所示

@Test
public void test() throws Exception {
    GregorianCalendar.from(ZonedDateTime.from(LocalDateTime.parse("2016-08-31T23:00:59")));
}
结果:

java.time.DateTimeException: Unable to obtain ZonedDateTime from TemporalAccessor: 2016-08-31T23:00:59 of type java.time.LocalDateTime
    at java.time.ZonedDateTime.from(ZonedDateTime.java:565)

您还必须提供
ZoneId
。我将不得不观察这一行为的原因。我会编辑和张贴,只要我看到它

LocalDateTime ldt = LocalDateTime.parse("2016-08-31T23:00:59");
GregorianCalendar gc = GregorianCalendar.from(ZonedDateTime.of(ldt, ZoneId.systemDefault()));
System.out.println(gc);
这导致:

java.util.GregorianCalendar[time=1472664659000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null],firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2016,MONTH=7,WEEK_OF_YEAR=35,WEEK_OF_MONTH=5,DAY_OF_MONTH=31,DAY_OF_YEAR=244,DAY_OF_WEEK=4,DAY_OF_WEEK_IN_MONTH=5,AM_PM=1,HOUR=11,HOUR_OF_DAY=23,MINUTE=0,SECOND=59,MILLISECOND=0,ZONE_OFFSET=19800000,DST_OFFSET=0]
编辑: 刚遇到这个有趣的

Java8java.time.*包是一个非常严格的包。它不允许类型和输入之间的灵活性-如果您想要ZonedDateTime对象,则必须从具有时区、日期和时间的输入构造它

为什么可以将LocalDateTime对象传递到 如果无法解析简单的 约会时间

和都实现了名为的接口,该接口扩展了

LocalDateTime
是ISO-8601日历系统中没有时区的日期时间,例如2007-12-03T10:15:30

ZoneDateTime
是ISO-8601日历系统中带有时区的日期时间,例如2007-12-03T10:15:30+01:00 Europe/Paris

现在,如果您看到(
LocalDateTime
ZonedDateTime
实现了超级接口
TemporalAccessor
temporalextenstedtemporalAccessor
)方法的
实现,这是正常的(即具有多态性行为)允许我们传递本地和分区日期时间

代码:

现在,我们来谈谈你的问题

您正在将
LocalDateTime.parse(“2016-08-31T23:00:59”)
传递给
from(TemporalAccessor temporal)
方法。因此,temporal不是
ZonedDateTime
的实例,它来自
ZoneId zone=ZoneId.from(temporal);

因此,由于
LocalDateTime
不包含 时区

如何解决这个问题

将区域ID与
ZonedDateTime
一起使用:

LocalDateTime ldt = LocalDateTime.parse("2016-08-31T23:00:59");
ZoneId zoneId = ZoneId.of("Europe/Paris");
ZonedDateTime zdt = ldt.atZone(zoneId);
GregorianCalendar gc = GregorianCalendar.from(zdt);
使用JUnit测试

@Test
public void test() throws Exception {
    ZoneId zoneId = ZoneId.of("Europe/Paris");
    GregorianCalendar.from(LocalDateTime.parse("2016-08-31T23:00:59").atZone(zoneId));
}

您是在问如何转换为
gregoriacalendar
,还是在问为什么它允许您传递
TemporalAccessor
接口的实现,并且只在运行时抱怨它?GeorgianCalendar需要时区,所以实际上您可以执行
gregoriacalendar.from(ZonedDateTime.parse)(“2016-08-31T23:00:59+00:00”))
提供时区。
ZonedDateTime.from
对于不同类型的输入和javadoc中提到的输入可能会失败。显示从旧日期时间类到java.time in到的转换的图表可能很有用。还可以找到在两个方向转换的示例代码。@BasilBourque这是一个非常棒的图表!
@Test
public void test() throws Exception {
    ZoneId zoneId = ZoneId.of("Europe/Paris");
    GregorianCalendar.from(LocalDateTime.parse("2016-08-31T23:00:59").atZone(zoneId));
}