700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 03Java常用API-19. Jdk8新增的日期Date API(LocalDateTime LocalDate LocalTime Instant Period Duration)

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

时间:2023-02-14 17:39:30

相关推荐

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

概述

LocalDateTime:包含了日期及时间。

LocalDate:不包含具体时间的日期。

LocalTime:不含日期的时间。

Instant:代表的是时间戳。

获取日期时间的信息

LocalDateTime

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

LocalDateTime nowDateTime = LocalDateTime.now();System.out.println("今天是:" + nowDateTime);//今天是:-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类获取日期信息。格式为 -08-04

LocalDate nowDate = LocalDate.now();System.out.println("今天的日期:" + nowDate);//今天的日期: -08-04int year = nowDate.getYear();//年:一般用这个方法获取年System.out.println("year:" + year);//year:int 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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。