LocalDate、 LocalTime、 LocalDateTime
LocalDate、 LocalTime、 LocalDateTime 类的实例是 不可变对象 ,分表表示使用 ISO-8601 日历系统的日期、时间、日期和时间。他们提供了简单的日期或时间,并不包含当前的时间信息。也不包含与时区相关的信息。
注:ISO-8601 日历系统是国际标准化组织制定的现代公民的日期和时间的表示法。说白了就是全球统一标准的表示方法。
示例1:
import org.junit.Test; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.Month; public class TestLocalDateTime { @Test public void test1(){ LocalDate localDate = LocalDate.now(); System.out.println(localDate);//输出:2020-06-25 LocalTime localTime = LocalTime.now(); System.out.println(localTime);//输出:23:12:59.211 LocalDateTime localDateTime = LocalDateTime.now(); System.out.println(localDateTime);//输出:2020-06-25T23:12:59.211 System.out.println("-------------------------------------------"); localDate = LocalDate.of(2022,1,1); System.out.println(localDate);//输出:2022-01-01 localDate = LocalDate.of(2023, Month.AUGUST,2); System.out.println("-------------------------------------------"); System.out.println(localDate);//输出:2023-08-02 localTime = LocalTime.of(1,2); System.out.println(localTime);//输出:01:02 localTime = LocalTime.of(1,2,3); System.out.println(localTime);//输出:01:02:03 localTime = LocalTime.of(1,2,3,4); System.out.println(localTime);//输出:01:02:03.000000004 System.out.println("-------------------------------------------"); localDateTime = LocalDateTime.of(localDate,localTime); System.out.println(localDateTime);//输出:2023-08-02T01:02:03.000000004 localDateTime = LocalDateTime.of(2024,1,1,1,1); System.out.println(localDateTime);//输出:2024-01-01T01:01 localDateTime = LocalDateTime.of(2024,1,1,1,1,1); System.out.println(localDateTime);//输出:2024-01-01T01:01:01 localDateTime = LocalDateTime.of(2024,1,1,1,1,1,1); System.out.println(localDateTime);//输出:2024-01-01T01:01:01.000000001 localDateTime = LocalDateTime.of(2024,Month.of(2),1,1,1,1,1); System.out.println(localDateTime);//输出:2024-02-01T01:01:01.000000001 } }
示例2:
@Test public void test2(){ LocalDate localDate = LocalDate.now(); System.out.println(localDate);//输出:2020-06-30 localDate = localDate.plusYears(1); System.out.println(localDate);//输出:2021-06-30 localDate = localDate.plusMonths(2); System.out.println(localDate);//输出:2021-08-30 localDate = localDate.plusDays(3); System.out.println(localDate);//输出:2021-09-02 localDate = localDate.plusWeeks(4); System.out.println(localDate);//输出:2021-09-30 localDate = localDate.plus(Period.ofDays(10));//加10天 System.out.println(localDate);//输出:2021-10-10 localDate = localDate.plus(Period.of(1,2,3));//加一年两个月三天 System.out.println(localDate);//输出:2022-12-13 localDate = localDate.plus(Period.ofYears(2));//加两年 System.out.println(localDate);//输出:2024-12-13 localDate = localDate.plus(1, ChronoUnit.DAYS);//加一天 System.out.println(localDate);//输出:2024-12-14 localDate = localDate.plus(1, ChronoUnit.MONTHS);//加一个月 System.out.println(localDate);//输出:2025-01-14 System.out.println("-------------------------------------------"); localDate = localDate.minusYears(1); System.out.println(localDate);//输出:2024-01-14 localDate.minusMonths(1); localDate.minusDays(1); localDate.minusWeeks(1); localDate.minus(Period.ofYears(2)); //..........其他用法和plus一样........... System.out.println("-------------------------------------------"); LocalTime localTime = LocalTime.now(); System.out.println(localTime);//输出:21:35:54.776 localTime = localTime.plusHours(1); System.out.println(localTime);//输出:22:35:54.776 //..........其他用法和localDate的一样........... //..........LocalDateTime的用法也一样........... }
示例3:
@Test public void test3(){ LocalDate localDate = LocalDate.now(); System.out.println(localDate);//输出:2020-06-30 System.out.println(localDate.getYear());//输出:2020 System.out.println(localDate.getDayOfMonth());//输出:30 //..........等....... //将天数、月份、等修改为指定的值 localDate = localDate.withDayOfMonth(22); System.out.println(localDate);//输出:2020-06-22 localDate = localDate.withMonth(11); System.out.println(localDate);//输出:2020-11-22 localDate = localDate.with(LocalDate.now()); System.out.println(localDate);//输出:2020-06-30 //..........等....... System.out.println("-------------------------------------------"); Period period = localDate.until(LocalDate.of(2020, 1, 1)); System.out.println(period.get(ChronoUnit.YEARS));//输出:0 相差 0 年 System.out.println(period.get(ChronoUnit.MONTHS));//输出:-5 相差 1 - 6 = -5 月 System.out.println(period.get(ChronoUnit.DAYS));//输出:-29 相差 1 - 30 = -29 天 System.out.println("-------------------------------------------"); System.out.println(localDate.isAfter(LocalDate.of(2020, 1, 1))); //输出:true System.out.println(localDate.isBefore(LocalDate.of(2020, 1, 1))); //输出:false System.out.println(localDate.isLeapYear());//是否闰年,输出:true }
Instant 时间戳
时间戳,以 Unix 元年:1970年1月1日00:00:00 到某个时间之间的毫秒值 。
示例:
@Test public void test4() { Instant ins1 = Instant.now(); // 默认获取UTC时区(世界协调时间,基本上是美国鬼子使用,和中国时间相差大概8小时) System.out.println(ins1);//输出结果:2020-06-30T14:28:05.217Z // 偏移量运算 OffsetDateTime offsetDateTime = ins1.atOffset(ZoneOffset.ofHours(8)); System.out.println(offsetDateTime);// 输出结果:2020-06-30T22:28:05.217+08:00 当前系统时间,国际标准的显示时间方式 // 获取时间戳 System.out.println(ins1.toEpochMilli());//输出结果:1593527285217 // 以Unix元年为起点,进行偏移量运算 Instant ins2 = Instant.ofEpochSecond(60); System.out.println(ins2);// 运行结果:1970-01-01T00:01:00Z }