当前位置: 代码迷 >> 综合 >> LocalDateTime JDK1.8
  详细解决方案

LocalDateTime JDK1.8

热度:76   发布时间:2023-12-03 23:27:46.0

文章目录

    • 一、使用新时间日期API的必要性
    • 二、新时间日期API常用、重要对象介绍
    • 三、新时间日期 API 详解与示例
      • 3.1 获取当前时间
      • 3.2 指定时间、日期创建对象
      • 3.3 日期、时间的加减
      • 3.4将年、月、日等修改为指定的值,并返回新的日期(时间)对象
      • 3.5获取日期的年月日周时分秒
      • 3.6时间日期前后的比较与判断
      • 3.7 判断是否是闰年
      • 3.8 java8 时钟 : clock()
      • 3.9 日期
      • 3.10计算时间(`Duration`)、日期间隔(`Period`)
      • 3.11时间日期的格式化(格式化后返回的类型是 String)
        • 3.11.1**使用 jdk 自身配置好的日期格式**
        • 3.11.2 自定义
      • 3.12 String -> LocalDataTime

一、使用新时间日期API的必要性

JDK8,以前,或许:

  • 有关时间日期的操作时,你会想到用Date
  • 日期、月份、天数相加减时,你会想到用Calenda
  • 时间日期进行格式化时,你会想到使用SimpleDateFormat或DateFormat下的其他子类

但是,你必须知道,以上有关的时间日期操作对象,都是可变的、线程不安全的,同时,如果作为一个经常写过类似代码的人来说,尽管有相关对象提供某些操作,但并不能很快、很简单的就能得到最终想要的结果,如:要计算两个时间点之间相差的年、月、日、周、时、分、秒等,这些计算尽管原有API也能够实现,但原有API除了线程不安全之外,另外一个不足之处就是代码繁琐,性能低。

二、新时间日期API常用、重要对象介绍

  • ZoneId: 时区ID,用来确定 Instant 和 LocalDateTime 互相转换的规则
  • Instant : 用来表示时间线上的一个点(瞬时)
  • LocalDate: 表示没有时区的日期, LocalDate 是不可变并且线程安全的
  • LocalTime: 表示没有时区的时间,LocalTime是不可变且线程安全的
  • LocalDateTime: 表示没有时区的日期时间, LocalDateTime 是不可变并且线程安全的
  • Clock: 用于访问当前时刻、日期、时间,用到时区
  • Duration : 用秒和纳秒表示时间的数量(长短),用于计算两个日期的 “时间” 间隔
  • Period: 用于计算两个日期间隔

三、新时间日期 API 详解与示例

3.1 获取当前时间

/ LocalDate.now() = 2021-06-09System.out.println("LocalDate.now() = " + LocalDate.now());//LocalTime.now() = 15:00:27.951System.out.println("LocalTime.now() = " + LocalTime.now());//LocalDateTime.now() = 2021-06-09T15:00:27.950System.out.println("LocalDateTime.now() = " + LocalDateTime.now());

3.2 指定时间、日期创建对象

        //LocalDate.of(2020,8,29) = 2020-08-29System.out.println("LocalDate.of(2020,8,29) = " + LocalDate.of(2020, 8, 29));//LocalTime.of(15,6,25,655) = 15:06:25.000000655System.out.println("LocalTime.of(15,6,25,655) = " + LocalTime.of(15, 6, 25, 655));//LocalDateTime.of(2016,7,10,16,54,55) = 2016-07-10T16:54:55System.out.println("LocalDateTime.of(2016,7,10,16,54,55) = " + LocalDateTime.of(2016, 7, 10, 16, 54, 55));

3.3 日期、时间的加减

  • 对于 LocalDate, 只有精度大于或等于日的加减,如年、月、日;

  • 对于 LocalTime, 只有精度小于或等于时的加减,如时、分、秒、纳秒;

  • 对于 LocalDateTime, 则可以进行任意精度的时间相加减;

    LocalDateTime now = LocalDateTime.now();//now.plusYears(1L) = 2022-06-09T15:20:17.968System.out.println("now.plusYears(1L) = " + now.plusYears(1L));//now.plusDays(1l) = 2021-06-10T15:20:17.968System.out.println("now.plusDays(1l) = " + now.plusDays(1L));//now.plusMonths(30L) = 2023-12-09T15:20:17.968System.out.println("now.plusMonths(30L) = " + now.plusMonths(30L));//now.plusHours(2L) = 2021-06-09T17:20:17.968System.out.println("now.plusHours(2L) = " + now.plusHours(2L));//now.plusMinutes(10L) = 2021-06-09T15:30:17.968 System.out.println("now.plusMinutes(10L) = " + now.plusMinutes(10L));

3.4将年、月、日等修改为指定的值,并返回新的日期(时间)对象

// 这个月的第一天//now.withDayOfMonth(1) = 2021-06-01T15:27:06.641System.out.println("now.withDayOfMonth(1) = " + now.withDayOfMonth(1));// 这年中的第100天//now.withDayOfYear(100) = 2021-04-10T15:27:06.641System.out.println("now.withDayOfYear(100) = " + now.withDayOfYear(100));// 指定年份//now.withYear(2022) = 2022-06-09T15:27:06.641System.out.println("now.withYear(2022) = " + now.withYear(2022));// 指定月份//now.withMonth(2) = 2021-02-09T15:27:06.641System.out.println("now.withMonth(2) = " + now.withMonth(2));

3.5获取日期的年月日周时分秒

//now.getDayOfMonth() = 9
System.out.println("now.getDayOfMonth() = " + now.getDayOfMonth());//now.getDayOfWeek() = WEDNESDAY
System.out.println("now.getDayOfWeek() = " + now.getDayOfWeek());//now.getDayOfYear() = 160
System.out.println("now.getDayOfYear() = " + now.getDayOfYear());//now.getHour() = 15
System.out.println("now.getHour() = " + now.getHour());//now.getSecond() = 30
System.out.println("now.getSecond() = " + now.getSecond());//now.getMonth() = JUNE
System.out.println("now.getMonth() = " + now.getMonth());//now.getMonthValue() = 6
System.out.println("now.getMonthValue() = " + now.getMonthValue());

3.6时间日期前后的比较与判断

LocalDateTime localDateTime1 = LocalDateTime.now();
LocalDateTime localDateTime2 = LocalDateTime.now().plusHours(1L);//localDateTime1.isAfter(localDateTime2) = false
System.out.println("localDateTime1.isAfter(localDateTime2) = " + localDateTime1.isAfter(localDateTime2));//localDateTime2.isBefore(localDateTime1) = false
System.out.println("localDateTime2.isBefore(localDateTime1) = " + localDateTime2.isBefore(localDateTime1));

3.7 判断是否是闰年

LocalDate localDate = LocalDate.now();
// 判断是否为闰年
//localDate.isLeapYear() = false
System.out.println("localDate.isLeapYear() = " + localDate.isLeapYear());

3.8 java8 时钟 : clock()

//返回当前时间,根据系统时间和UTC
//Clock.systemUTC() = SystemClock[Z]
System.out.println("Clock.systemUTC() = " + Clock.systemUTC());

3.9 日期

Instant instant = Instant.now();
//instant = 2021-06-09T07:47:15.247Z
System.out.println("instant = " + instant);Date date = new Date(System.currentTimeMillis());
//date = Wed Jun 09 15:47:15 CST 2021
System.out.println("date = " + date);// instant -> date
Date date1 = Date.from(instant);
//date1 = Wed Jun 09 15:50:16 CST 2021
System.out.println("date1 = " + date1);// date -> instant
Instant instant1 = date1.toInstant();
//instant1 = 2021-06-09T07:50:16.285Z
System.out.println("instant1 = " + instant1);

3.10计算时间(Duration)、日期间隔(Period)

LocalDate localDate1 = LocalDate.of(2019, 10, 10);
LocalDate localDate2 = LocalDate.of(2018, 3, 13);
//localDate1 - localDate2
Period period = Period.between(localDate2,localDate1);//period.getYears() = 1
System.out.println("period.getYears() = " + period.getYears());//period.getMonths() = 6
System.out.println("period.getMonths() = " + period.getMonths());//period.getDays() = 27
System.out.println("period.getDays() = " + period.getDays());//period.get(ChronoUnit.YEARS) = 1
System.out.println("period.get(ChronoUnit.YEARS) = " + period.get(ChronoUnit.YEARS));LocalTime localTime1 = LocalTime.of(6, 3, 33);
LocalTime localTime2 = LocalTime.of(22, 22, 22);//localDateTime2 - localDateTime1
Duration duration = Duration.between(localDateTime1,localDateTime2);//duration.toDays() = 0
System.out.println("duration.toDays() = " + duration.toDays());//duration.toHours() = 1
System.out.println("duration.toHours() = " + duration.toHours());//duration.getNano() = 0
System.out.println("duration.getNano() = " + duration.getNano());//duration.toMinutes() = 60
System.out.println("duration.toMinutes() = " + duration.toMinutes());

3.11时间日期的格式化(格式化后返回的类型是 String)

3.11.1使用 jdk 自身配置好的日期格式

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.BASIC_ISO_DATE;
String format = LocalDateTime.now().format(dateTimeFormatter);//format = 20210609
System.out.println("format = " + format);

3.11.2 自定义

DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
String format1 = LocalDateTime.now().format(dateTimeFormatter1);//format1 = 2021/06/09 16:20:0
System.out.println("format1 = " + format1);
  • LocalDate 只能设置仅含年月日的格式
  • LocalTime 只能设置仅含时分秒的格式
  • LocalDateTime 可以设置含年月日时分秒的格式

3.12 String -> LocalDataTime

//2018-01-13 21:27:30 对应 yyyy-MM-dd HH:mm:ss
//20180113213328 对应 yyyyMMddHHmmss
String datetime =  "2018-01-13 21:27:30";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime ldt = LocalDateTime.parse(datetime, dtf);
System.out.println(ldt);

参考:https://www.jianshu.com/p/048ee8580639