新增日期类


JDK8新增日期类

从Java 8开始,java.time包提供了新的日期和时间API,主要涉及的类型有:

  • LocalDate:不包含具体时间的日期
  • LocalTime:不含日期的时间
  • LocalDateTime:包含了日期及时间
  • Instant:代表的是时间戳
  • DateTimeFormatter用于做时间的格式化和解析的
  • Duration:用于计算两个“时间”间隔
  • Period:用于计算两个“日期”间隔
  • 新增的API严格区分了时刻、本地日期、本地时间,并且,对日期和时间进行运算更加方便。
  • 其次,新API的类型几乎全部是不变类型(和String的使用类似),可以放心使用不必担心被修改。

LocalDate、LocalTime、LocalDateTime

  • 他们分别表示日期,时间,,日期时间对象,他们的类的实例是不可变的对象。

  • 他们三者构建对象和API都是通用的

    根据当前时间创建对象:LocalDateTime.now()

    指定日期时间对象: LocalDateTime.of(2022,5,26,6,20)

    获取时间信息:getYear()、getHour()等

转换API

LocalDateTime可以转换为LocalDate或LocalTime

方法名 说明
public LocalDate toLocalDate() 转换成一个LocalDate对象
public LocalTime toLocalTime() 转换成一个LocalTime对象

修改相关的API

这些方法返回的是一个新的实例引用,因为LocalDateTime、LocIDate 、LocalTime都是不可变的。

方法名 说明
plusDays, plusWeeks, plusMonths, plusYears 向当前 LocalDate 对象添加几天,几周、几个月、几年
minusDays, minusWeeks, minusMonths, minusYears 从当前 LocalDate 对象减去几天、几周、几个月、几年
withDayofMonth, withDayofYear, withMonth, withYear 将月份天数、年份天数、月份、年份修改为指定的值井适回新的LocalDate 对象
isBefore, isAfter 比较两个 LocalDate

Instant时间戳

  • JDK8获取时间戳特别简单,且功能更丰富。Instant类由一个静态的工厂方法now()可以返回当前时间戳

    Instant instant = Instant.now();
    System.out.println("当前时间戳是:" + instant);
    Date date = Date.from(instant);
    System.out.println("当前时间戳是:" + date);
    instant = date.toInstant();
    System.out.println(instant);
  • 时间戳是包含日期和时间的,与java.utiLDate很类似,事实上Instant就是类似IDK8 以前的Date。

  • Instant和Date这两个类可以进行转换。

注意时区:instant.atZone(ZoneId.systemDefault())

DateTimeFormatter

  • 在JDK8中,引入了一个全新的日期与时间格式器DateTimeFormatter。
  • 正反都能调用format方法。
LocalDateTime ldt = LocalDateTime.now();
System.out.println(ldt);//2021-03-01T15:09:17.444190900
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
StringldtStr = ldt.format(dtf);
System.out.println(ldtstr);//2021-03-01 15:09:17
StringldtStr1 = dtf.format(ldt);
System.out.println(ldtStr1);//2021-03-01 15:09:17

Period

  • 在Java8中,我们可以使用以下类来计算日期间隔差异:java.time.Period
  • 主要是Period类方法 getYears(), getMonths()和 getDays() 来计算,只能精确到年月日。
  • 用于 LocalDate 之间的比较。
LocalDate today = LocalDate.now();
System.out.println(today)//2021-03-01
LocalDate birthDate = LocalDate.Of(1995, 1, 11);
System.out.println(birthDate); // 1995-01-11
Period period = Period.between(birthDate,today);
system.out.printf("年龄 : %d 年 %d 月 %d 日", period.getYears(), period.getHonths(),period.getDays());

Duration

  • 在Java8中,我们可以使用以下类来计算时间间隔差异:java.time.Duration
  • 提供了使用基于时间的值测量时间量的方法。
  • 用于 LocalDateTime之间的比较。也可用于 Instant 之间的比较。
LocalDateTime today = LocalDateTime.now();
System.out.println(today);
LocalDateTime birthDate = LocalDateTime.of(1990,10,1,18,50,30);
System.out.println(birthDate);
Duration duration = Duration.between(birthDate,today);//第二个参数减第一个参数
System.out.println(duration.toDays());//两个时间差的天数
System.out.println(duration.toHours());//两个时间差的小时数
System.out.println(duration.toMinutes());//两个时间差的分钟数
System.out.println(duration.toMillis())://两个时间差的毫秒数
System.out.println(duration.toNanos());//两个时间差的纳秒数

与Period区别:Duration每一个值都是总和,而Period是所有值加起来的总和


文章作者: 泷少
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 泷少 !
评论
  目录