当前位置: 代码迷 >> 综合 >> 03Java常用API-19. Jdk8新增的日期Date API(LocalDateTime,LocalDate,LocalTime,Instant,Period,Duration)
  详细解决方案

03Java常用API-19. Jdk8新增的日期Date API(LocalDateTime,LocalDate,LocalTime,Instant,Period,Duration)

热度:18   发布时间:2024-02-06 16:16:22.0

概述

LocalDateTime:包含了日期及时间。
LocalDate:不包含具体时间的日期。
LocalTime:不含日期的时间。
Instant:代表的是时间戳。

获取日期时间的信息

LocalDateTime

LocalDateTime类获取日期时间信息。格式为 2020-08-04T16:25:34.250

LocalDateTime nowDateTime = LocalDateTime.now();System.out.println("今天是:" + nowDateTime);//今天是:2020-08-04T16:25:34.250System.out.println(nowDateTime.getYear());//年System.out.println(nowDateTime.getMonthValue());//月System.out.println(nowDateTime.getDayOfMonth());//日System.out.println(nowDateTime.getHour());//时System.out.println(nowDateTime.getMinute());//分System.out.println(nowDateTime.getSecond());//秒System.out.println(nowDateTime.getNano());//纳秒//日:当年的第几天System.out.println("dayOfYear:" + nowDateTime.getDayOfYear());//dayOfYear:228//星期System.out.println(nowDateTime.getDayOfWeek());//THURSDAYSystem.out.println(nowDateTime.getDayOfWeek().getValue());//4//月份System.out.println(nowDateTime.getMonth());//SEPTEMBERSystem.out.println(nowDateTime.getMonth().getValue());//8

LocalDate

LocalDate类获取日期信息。格式为 2020-08-04

LocalDate nowDate = LocalDate.now();System.out.println("今天的日期:" + nowDate);//今天的日期: 2020-08-04int year = nowDate.getYear();//年:一般用这个方法获取年System.out.println("year:" + year);//year:2020int month = nowDate.getMonthValue();//月:一般用这个方法获取月System.out.println("month:" + month);//month:8int day = nowDate.getDayOfMonth();//日:当月的第几天,一般用这个方法获取日System.out.println("day:" + day);//day:4int dayOfYear = nowDate.getDayOfYear();//日:当年的第几天System.out.println("dayOfYear:" + dayOfYear);//dayOfYear:249//星期System.out.println(nowDate.getDayOfWeek());//THURSDAYSystem.out.println(nowDate.getDayOfWeek().getValue());//4//月份System.out.println(nowDate.getMonth());//SEPTEMBERSystem.out.println(nowDate.getMonth().getValue());//8

LocalTime

LocalTime类获取时间信息。格式为 15:33:56.749

		LocalTime nowTime = LocalTime.now();System.out.println("今天的时间:" + nowTime);//今天的时间:15:33:56.749int hour = nowTime.getHour();//时System.out.println("hour:" + hour);//hour:15int minute = nowTime.getMinute();//分System.out.println("minute:" + minute);//minute:33int second = nowTime.getSecond();//秒System.out.println("second:" + second);//second:56int nano = nowTime.getNano();//纳秒System.out.println("nano:" + nano);//nano:749000000
  相关解决方案