当前位置: 代码迷 >> 综合 >> Java基础 -> 获取时间1.8新增类(LocalDate,LocalDateTime,localTime类,instant)
  详细解决方案

Java基础 -> 获取时间1.8新增类(LocalDate,LocalDateTime,localTime类,instant)

热度:22   发布时间:2023-12-16 10:13:04.0
/*** LocalDate,LocalDateTime,localTime类*/@Testpublic void local(){
    //static LocalDate now()//从默认时区的系统时钟获取当前日期。LocalDate localDate = LocalDate.now();System.out.println(localDate);//2020-09-03//static LocalDate now()//从默认时区的系统时钟获取当前的日期时间。LocalDateTime localDateTime = LocalDateTime.now();System.out.println(localDateTime);//2020-09-03T17:16:15.270//int getDayOfMonth()//获取月份字段。//DayOfWeek getDayOfWeek()//获取星期几字段,这是一个枚举 DayOfWeek 。//int getDayOfYear()//获得日期字段。//int getHour()//获取时间字段。System.out.println(localDateTime.getDayOfMonth());System.out.println(localDateTime.getDayOfWeek());System.out.println(localDateTime.getDayOfYear());System.out.println(localDateTime.getHour());LocalTime localTime = LocalTime.now();System.out.println(localTime);//17:23:37.642}
}

附加

    /*** instant*/@Testpublic void instant(){
    //static Instant now()//从系统时钟获取当前瞬间。Instant instant = Instant.now();System.out.println(instant);//2020-09-03T09:38:06.694Z,不是北京时间,少8h}
  相关解决方案