一、时间和日期API介绍
Java三次引入处理时间的API,JDK1.0中包含了一个Date
类,但大多数方法在java1.1引入Calendear
类之后被弃用了。 它的实例都是可变的,而且它的API很难使用,比如月份是从0开始这种反人类的设置。java8引入的java.time 包中
API 已经纠正了之前的问题。它已经完全实现了JSR310
规范。
java8引入了一套全新的时间日期API,本篇随笔将说明学习java8的这套API。
java.time包中的是类是不可变(类似String类,改变后会新建一个对象)且线程安全的。新的时间及日期API位于java.time中,下面是一些关键类
●Instant——它代表的是时间戳
●LocalDate——不包含具体时间的日期,比如2014-01-14。它可以用来存储生日,周年纪念日,入职日期等。
●LocalTime——它代表的是不含日期的时间
●LocalDateTime——它包含了日期及时间,不过还是没有偏移信息或者说时区。
●ZonedDateTime——这是一个包含时区的完整的日期时间,偏移量是以UTC/格林威治时间为基准的。
二、常用时间日期类介绍
1、Instant 时间戳
1 // Instant:时间戳(以Unix元年:1970年1月1日 00:00:00.000 到某个时间之间的毫秒值) 2 @Test 3 public void test1() { 4 // 当前时间戳 5 Instant now = Instant.now(); 6 System.out.println("now = " + now); 7 System.out.println("now.getEpochSecond() = " + now.getEpochSecond()); 8 System.out.println("now.toEpochMilli() = " + now.toEpochMilli()); 9 10 LocalDateTime time = LocalDateTime.of(1970, 1, 1, 0,0); 11 System.out.println("time = " + time); 12 Instant instant = time.toInstant(ZoneOffset.UTC); 13 System.out.println("instant = " + instant); 14 System.out.println("instant.toEpochMilli() = " + instant.toEpochMilli()); 15 System.out.println("instant.getEpochSecond() = " + instant.getEpochSecond()); 16 17 Instant instant1 = Instant.ofEpochSecond(60); 18 System.out.println("instant1 = " + instant1); 19 }
2、LocalDate 本地日期
1 @Test 2 public void test1() { 3 // 当前日期 4 LocalDate localDate = LocalDate.now(); 5 System.out.println("localDate = " + localDate); 6 7 LocalDateTime localDateTime = LocalDateTime.now(); 8 System.out.println("localDateTime = " + localDateTime); 9 String s = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")); 10 System.out.println("s = " + s); 11 }
3、LocalTime 本地时间
1 @Test 2 public void test1() { 3 // 当前本地时间 4 LocalTime now = LocalTime.now(); 5 System.out.println("now = " + now); 6 7 LocalTime time = LocalTime.of(12, 30, 59); 8 System.out.println("time = " + time); 9 System.out.println("time.getHour() = " + time.getHour()); 10 System.out.println("time.getMinute() = " + time.getMinute()); 11 System.out.println("time.getSecond() = " + time.getSecond()); 12 }
4、LocalDateTime 本地日期时间
1 @Test 2 public void test1() { 3 LocalDate localDate = LocalDate.now(); 4 System.out.println("localDate = " + localDate); 5 6 // 当前本地日期时间 7 LocalDateTime localDateTime = LocalDateTime.now(); 8 System.out.println("localDateTime = " + localDateTime); 9 String s = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")); 10 System.out.println("s = " + s); 11 } 12 13 @Test 14 public void test2() { 15 LocalDateTime dateTime = LocalDateTime.of(2024, 12, 4, 8, 9, 14); 16 System.out.println("dateTime = " + dateTime); 17 18 LocalDateTime dateTime1 = dateTime.plusYears(2); 19 System.out.println("dateTime1 = " + dateTime1); 20 21 LocalDateTime dateTime2 = dateTime.minusHours(2); 22 System.out.println("dateTime2 = " + dateTime2); 23 24 System.out.println(dateTime.getYear()); 25 System.out.println(dateTime.getMonth()); 26 System.out.println(dateTime.getDayOfMonth()); 27 System.out.println(dateTime.getHour()); 28 System.out.println(dateTime.getMinute()); 29 System.out.println(dateTime.getSecond()); 30 } 31 32 // Instant:时间戳(以Unix元年:1970年1月1日 00:00:00.000 到某个时间之间的毫秒值) 33 @Test 34 public void test3() { 35 Instant now = Instant.now(); 36 System.out.println("now = " + now); 37 System.out.println("now.getEpochSecond() = " + now.getEpochSecond()); 38 System.out.println("now.toEpochMilli() = " + now.toEpochMilli()); 39 40 LocalDateTime time = LocalDateTime.of(1970, 1, 1, 0,0); 41 System.out.println("time = " + time); 42 Instant instant = time.toInstant(ZoneOffset.UTC); 43 System.out.println("instant = " + instant); 44 System.out.println("instant.toEpochMilli() = " + instant.toEpochMilli()); 45 System.out.println("instant.getEpochSecond() = " + instant.getEpochSecond()); 46 47 Instant instant1 = Instant.ofEpochSecond(60); 48 System.out.println("instant1 = " + instant1); 49 50 }
5、ZonedDateTime 时区日期时间
1 @Test 2 public void test1() { 3 ZoneId.getAvailableZoneIds().stream().sorted() 4 .forEach(System.out::println); 5 } 6 7 @Test 8 public void test2() { 9 LocalDateTime now = LocalDateTime.now(ZoneId.of("Asia/Shanghai")); 10 System.out.println("now = " + now); 11 ZonedDateTime zonedDateTime = now.atZone(ZoneId.of("Asia/Shanghai")); 12 System.out.println("zonedDateTime = " + zonedDateTime); 13 14 }
6、Duration 和 Period 时间间隔
Duration:计算两个“时间”之间的间隔
Period:计算两个“日期”之间的间隔
1 // Duration:计算两个“时间”之间的间隔 2 // Period:计算两个“日期”之间的间隔 3 @Test 4 public void test4() throws InterruptedException { 5 Instant ins1 = Instant.now(); 6 System.out.println("ins1 = " + ins1); 7 Thread.sleep(1234); 8 Instant ins2 = Instant.now(); 9 System.out.println("ins2 = " + ins2); 10 Duration between = Duration.between(ins1, ins2); 11 System.out.println("between = " + between); 12 System.out.println("between.toMillis() = " + between.toMillis()); 13 System.out.println("between.toNanos() = " + between.toNanos()); 14 15 16 17 LocalDateTime localDateTime1 = LocalDateTime.now(); 18 Thread.sleep(1234); 19 LocalDateTime localDateTime2 = LocalDateTime.now(); 20 Duration between1 = Duration.between(localDateTime1, localDateTime2); 21 System.out.println("between1 = " + between1); 22 System.out.println("between1.toMillis() = " + between1.toMillis()); 23 24 } 25 26 @Test 27 public void test5() throws InterruptedException { 28 LocalDate localDate1 = LocalDate.now(); 29 Thread.sleep(1234); 30 LocalDate localDate2 = LocalDate.of(2022,7,30); 31 32 Period between = Period.between(localDate1, localDate2); 33 System.out.println("between = " + between); 34 System.out.println("between.toMillis() = " + between.getDays()); 35 System.out.println("between.getMonths() = " + between.getMonths()); 36 System.out.println("between.getYears() = " + between.getYears()); 37 }
7、DateTimeFormatter 日期时间格式化
1 @Test 2 public void test1() { 3 LocalDateTime now = LocalDateTime.now(); 4 DateTimeFormatter basicIsoDate = DateTimeFormatter.BASIC_ISO_DATE; 5 6 String format1 = now.format(basicIsoDate); 7 System.out.println("format1 = " + format1); 8 9 String format2 = now.format(DateTimeFormatter.ISO_DATE_TIME); 10 System.out.println("format2 = " + format2); 11 12 DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); 13 String format3 = now.format(dateTimeFormatter); 14 System.out.println("format3 = " + format3); 15 16 LocalDateTime parse = LocalDateTime.parse(format3, dateTimeFormatter); 17 System.out.println("parse = " + parse); 18 }
8、TemporalAdjuster 时间调整器
1 // TemporalAdjuster 时间调整器 2 @Test 3 public void test6() { 4 LocalDateTime dateTime = LocalDateTime.now(); 5 System.out.println("dateTime = " + dateTime); 6 7 LocalDateTime dateTime1 = dateTime.withDayOfMonth(10); 8 System.out.println("dateTime1 = " + dateTime1); 9 10 // 使用时间调整器 11 LocalDateTime dateTime2 = dateTime.with(TemporalAdjusters.firstDayOfMonth()); 12 System.out.println("dateTime2 = " + dateTime2); 13 14 // 自定义时间调整器 15 LocalDateTime dateTime3 = dateTime.with(time -> ((LocalDateTime) time).plusDays(8)); 16 System.out.println("dateTime3 = " + dateTime3); 17 }