LocalDate,LocalTime,LocalDateTime之间的转化和常用应用

Java8中,对于日期、时间、时间日期有不同的对象来表示,分别就是LocalDate、LocalTime、LocalDateTime

他们都位于java.time包下,并且他们都仅单纯的表示一个不可变的时间对象,无时区等附加信息的出现

LocalDate
一个不可变的时间对象用来表示一个日期(年月日),仅包含日期,例如2020-06-06

它的可表示范围为-999999999-01-01到+999999999-12-31

LocalTime
一个不可变的时间对象,用来表示一个时间(时分秒),最高精度为纳秒,例如11:11:11
它的表示范围为:00:00到23:59:59.999999999

LocalDateTime
一个不可变的时间对象,用来表示一个日期时间(年月日时分秒),最高精度为纳秒,例如2020-06-06T13:45:30.123456789
-999999999-01-01T00:00:00到+999999999-12-31T23:59:59.999999999

LocalDate常用方法示例
LocalDate、LocalTime、LocalDateTime三者的使用方式基本相同,APi设计也类似,可以说掌握其中一个明白其他两个的用法了

此处以LocalDate的常用方法进行介绍:

初始化一个LocalDate
now(…)系列: 获取当前日期
of(…)系列 : 获取指定年月日日期
parse(…)系列: 将日期字符串转换为LocalDate日期
from(…)系列:将TemporalAccessor类型对象转换为LocalDate日期

LocalDate localDate = LocalDate.now();
System.out.println(localDate.getYear());//获取年
System.out.println(localDate.getMonthValue());//
System.out.println(localDate.getDayOfMonth());//
System.out.println(localDate.getMonth());//月份
System.out.println(localDate.getDayOfWeek());//星期
System.out.println(localDate.getDayOfYear());//本年中的第几天

System.out.println("是否闰年:"+localDate.isLeapYear());
System.out.println("本月天数:"+localDate.lengthOfMonth());
System.out.println("本年天数:"+localDate.lengthOfYear());
LocalDate localDate = LocalDate.now();
System.out.println("当前日期:"+localDate);
System.out.println("3天后:"+localDate.plusDays(3));
System.out.println("1月后:"+localDate.plusMonths(1));
System.out.println("1年后:"+localDate.plusYears(1));
System.out.println("1周后:"+localDate.plusWeeks(1));
System.out.println("3天前:"+localDate.minusDays(3));
System.out.println("1月前:"+localDate.minusMonths(1));
System.out.println("1年前:"+localDate.minusYears(1));
System.out.println("1周前:"+localDate.minusWeeks(1));

System.out.println(localDate.withYear(2012));//指定某一年的今天
System.out.println(localDate.withMonth(12));//指定某月的今天
System.out.println(localDate.withDayOfMonth(1));//指定本月的第几天
System.out.println(localDate.withDayOfYear(1));//指定今年的第几天
LocalDate localDate = LocalDate.of(2020,1,1);
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime = LocalDateTime.now();

LocalDate ldt2ld = localDateTime.toLocalDate();
LocalTime ldt2lt = localDateTime.toLocalTime();
//localDateTime--->LocalDate
System.out.println(localDateTime+"---> localDateTime.toLocalDate() --->"+ldt2ld);
//localDateTime--->LocalTime
System.out.println(localDateTime+"---> localDateTime.toLocalTime() --->"+ldt2lt);

LocalDateTime atDate = localTime.atDate(localDate);
LocalDateTime atTime = localDate.atTime(localTime);
LocalDateTime dateTime = LocalDateTime.of(localDate, localTime);
//localDate+localTime--->LocalDateTime
System.out.println("["+localDate+" + "+localTime+"] localDate.atTime(localTime) --->"+atTime);
System.out.println("["+localDate+" + "+localTime+"] localTime.atDate(localDate) --->"+atDate);
System.out.println("["+localDate+" + "+localTime+"] LocalDateTime.of(localDate, localTime) --->"+dateTime);
LocalDateTime localDateTime = LocalDateTime.of(2020, 11, 11, 11, 11, 59);//2020-11-11 11:11:59
LocalDateTime localDateTime1 = localDateTime.plusSeconds(1);//2020-11-11 11:11:59 +1秒,localDateTime1=2020-11-11 11:12:00
LocalDateTime localDateTime2 = localDateTime.plusSeconds(-1);//2020-11-11 11:11:59 -1秒,localDateTime2=2020-11-11 11:11:58
LocalDateTime localDateTime3 = localDateTime.plusMinutes(1);//2020-11-11 11:11:59 +1分钟,localDateTime3=2020-11-11 11:12:59
LocalDateTime localDateTime4 = localDateTime.plusMinutes(-1);//2020-11-11 11:11:59 -1分钟,localDateTime4=2020-11-11 11:10:59
LocalDateTime localDateTime5 = localDateTime.plusHours(1);//2020-11-11 11:11:59 +1小时,localDateTime5=2020-11-11 12:11:59
LocalDateTime localDateTime6 = localDateTime.plusHours(-1);//2020-11-11 11:11:59 -1小时,localDateTime6=2020-11-11 10:11:59
LocalDateTime localDateTime7 = localDateTime.plusDays(1);//2020-11-11 11:11:59 +1天,localDateTime7=2020-11-12 11:11:59
LocalDateTime localDateTime8 = localDateTime.plusDays(-1);//2020-11-11 11:11:59 -1天,localDateTime8=2020-11-10 11:11:59
LocalDateTime localDateTime9 = localDateTime.plusMonths(1);//2020-11-11 11:11:59 +1月,localDateTime9=2020-12-11 11:11:59
LocalDateTime localDateTime10 = localDateTime.plusMonths(-1);//2020-11-11 11:11:59 -1月,localDateTime10=2020-10-11 11:11:59
LocalDateTime localDateTime11 = localDateTime.plusYears(1);//2020-11-11 11:11:59 +1年,localDateTime11=2021-11-11 11:11:59
LocalDateTime localDateTime12 = localDateTime.plusYears(-1);//2020-11-11 11:11:59 -1年,localDateTime12=2019-11-11 11:11:59

 

posted @ 2022-11-21 01:31  张亮java  阅读(1017)  评论(0编辑  收藏  举报