Enums Java 8:ZonedDateTime不支持enum Month,但它将接受包含enum Month的DataTime对象
我正在处理Enums Java 8:ZonedDateTime不支持enum Month,但它将接受包含enum Month的DataTime对象,enums,java-8,zoneddatetime,Enums,Java 8,Zoneddatetime,我正在处理ZonedDateTime,同时将参数传递给ZonedDateTime.of(1990年,一月,20,10,20,30400,zoneId),它不会以Month.janur作为参数,但是当我用enum Month.janur传递DateTime对象时,它会正常工作。为什么ZonedDateTime.of()方法将不支持enum Month.janur 将来,他们是否将添加支持ZoneDateTime.of方法的enum Month.Janur 示例: package com.katte
ZonedDateTime
,同时将参数传递给ZonedDateTime.of(1990年,一月,20,10,20,30400,zoneId)
,它不会以Month.janur
作为参数,但是当我用enum Month.janur
传递DateTime
对象时,它会正常工作。为什么ZonedDateTime.of()
方法将不支持enum Month.janur
将来,他们是否将添加支持ZoneDateTime.of
方法的enum Month.Janur
示例:
package com.katte.infa;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class DateTimeDemo {
public static void main(String[] args) {
LocalDateTime dateTime1 = LocalDateTime.of(1990, 1, 1,12,20,20);
System.out.println("DateTime1:" +dateTime1);
LocalDateTime dateTime2 = LocalDateTime.of(1990, Month.JANUARY, 1,12,20,20); // enum Month.JANUARY
System.out.println("DateTime2:" +dateTime2);
ZoneId zoneId = ZoneId.systemDefault();
ZonedDateTime zdateTime1 = ZonedDateTime.of(dateTime1,zoneId);
ZonedDateTime zdateTime2 = ZonedDateTime.of(dateTime2,zoneId); // enum Month.JANUARY, works fine
System.out.println("ZdateTime1 :" +zdateTime1);
System.out.println("ZdateTime2 :" +zdateTime2);
ZonedDateTime zdateTime3 = ZonedDateTime.of(1990,10,20,10,20,30,400,zoneId);
ZonedDateTime zdateTime4 = ZonedDateTime.of(1990,Month.JANUARY,20,10,20,30,400,zoneId); // not compile
}
}
LocalDateTime.of()
的签名如下:
public static LocalDateTime of(int year, Month month, int dayOfMonth,
int hour, int minute, int second)
而zoneDateTime.of()
的是:
public static ZonedDateTime of(
int year, int month, int dayOfMonth,
int hour, int minute, int second, int nanoOfSecond, ZoneId zone)
如您所见,ZonedDateTime.of()
的第二个参数int-month
接受int
,您不能在其中传递month
实例
但是LocalDateTime.of()
的第二个参数Month-Month
接受Month
如果要在ZoneDateTime.of()
中使用Month
,可以通过Month.JANUARY.getValue()
使用它。函数的getValue()
返回1-12
范围内的int
,这是ZonedDateTime.of()的月份参数的有效范围。以下示例很好地工作:
ZonedDateTime.of(1990, Month.JANUARY.getValue(),20,10,20,30,400,null);
Arvind Katte,请原谅提醒一下,你忘记接受答案了吗?看起来你去年做得很严格,但今年还没有做。