Java8新日期时间处理API

日期时间的处理

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

请添加图片描述

新增的API严格区分了时刻、本地日期、本地时间,并且,对日期和时间进行运算更加方便。

其次,新API的类型几乎全部是不变类型(和String的使用类似),可以放心使用不必担心被修改。

1、LocalDate、LocalTime、LocalDateTime

1.1、构造器

方法名说明
public static Xxxx now();静态方法,根据当前时间创建对象LocaDate localDate = LocalDate.now(); LocalTime llocalTime = LocalTime.now(); LocalDateTime localDateTime = LocalDateTime.now();
public static Xxxx of(…);静态方法,指定日期/时间创建对象LocalDate localDate1 = LocalDate.of(2099 , 11,11); LocalTime localTime1 = LocalTime.of(11, 11, 11); LocalDateTime localDateTime1 = LocalDateTime.of(2020, 10, 6, 13, 23, 43);

1.2、常用方法

方法名说明
public int geYear()获取年
public int getMonthValue()获取月份(1-12)
Public int getDayOfMonth()获取月中第几天乘法
Public int getDayOfYear()获取年中第几天
Public DayOfWeek getDayOfWeek()获取星期

LocalDateTime的转换API

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

LocalDateTime 综合了 LocalDate 和 LocalTime 里面的方法,所以下面只用 LocalDate 和 LocalTime 来举例。

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

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

1.3、案例

2、Instance时间戳

时间戳就是获取当前时间,JDK8获取时间戳特别简单,且功能更丰富。Instant类由一个静态的工厂方法now()可以返回当前时间戳。时间戳是包含日期和时间的,与java.util.Date很类似,事实上Instant就是类似JDK8 以前的Date。Instant和Date这两个类可以进行转换。

@Test
public void date2(){
    // 获取一个时间戳对象
    Instant instant = Instant.now();
    // 2023-02-20T06:20:08.085Z 返回的是东八区的时间需要加8小时
    System.out.println(instant);

    // 返回系统时间戳
    Instant instant1 = Instant.now();
    instant1.atZone(ZoneId.systemDefault());
    System.out.println(instant1);
    
    // 时间戳转为Date
    Date date = Date.from(instant1);
    System.out.println(date);

    // Date转为时间戳
    Date date1 = new Date();
    System.out.println(date1.toInstant());
}

3、日期时间格式化

DateTimeFormatter

// 时间格式化
@Test
public void date3(){
    // 获取当地时间
    LocalDateTime localDateTime = LocalDateTime.now();
    System.out.println(localDateTime);

    // 格式化时间
    // 创建格式化器
    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    // 正向使用
    System.out.println(dateTimeFormatter.format(localDateTime));
    // 反向使用
    System.out.println(localDateTime.format(dateTimeFormatter));

    // 解析时间
    String dateTime = "2023-02-25 14:34:46";
    // 按yyyy-MM-dd HH:mm:ss 模板将前端传来的数据进行解析,返回一个LocalDateTime对象
    LocalDateTime localDateTime1 = LocalDateTime.parse(dateTime,dateTimeFormatter);
    System.out.println(localDateTime1);
    System.out.println(localDateTime1.getDayOfYear());

}

4、计算日期间隔差异

4.1、Period

通常用来获取年月日的差

// 获取时间差
@Test
public void date4(){
  //  创建当前时间
    LocalDate ld = LocalDate.now();
    // 创建过去时间
    LocalDate ld1 = LocalDate.of(1999,8,18);
    // 创建日期间隔对象
    Period period = Period.between(ld1,ld);
    // 获取两个时间的间隔  23年6个月2天
    System.out.println(period.getYears());//年  
    System.out.println(period.getMonths());  //月
    System.out.println(period.getDays());//日
}

4.2、Duration

通常用来获取时分秒的差

// Duration 获取时间间隔
@Test
public void date5(){
    // 创建当前时间
    LocalDateTime ldt = LocalDateTime.now();
    // 创建已给对比的过去时间
    LocalDateTime ldt1 = LocalDateTime.of(1999,8,18,12,12,12);
    // 对比时间返回Duration
    Duration d = Duration.between(ldt1,ldt);
    System.out.println(d.toDays()); // 相差 天数
    System.out.println(d.toMillis());//相差 秒数
    System.out.println(d.toHours());//相差小时数
    System.out.println(d.toNanos());//相差毫秒数
    System.out.println(d.toMinutes());//相差分钟数
}

4.3、lChronoUnit

lChronoUnit类可用于在单个时间单位内测量一段时间,这个工具类是最全的了,可以用于比较所有的时间单位

@Test
public void date6(){
    LocalDateTime today = LocalDateTime.now();
    System.out.println(today);
    // 出生日期
    LocalDateTime birthDate = LocalDateTime.of(1999,8,18,8,8,8);
    System.out.println(birthDate);

    System.out.println("相差的年数"+ ChronoUnit.YEARS.between(birthDate,today));
    System.out.println("相差的月数"+ ChronoUnit.MONTHS.between(birthDate,today));
    System.out.println("相差的周数"+ ChronoUnit.WEEKS.between(birthDate,today));
    System.out.println("相差的天数"+ ChronoUnit.DAYS.between(birthDate,today));
    System.out.println("相差的时数"+ ChronoUnit.HOURS.between(birthDate,today));
    System.out.println("相差的分数"+ ChronoUnit.MINUTES.between(birthDate,today));
    System.out.println("相差的秒数"+ ChronoUnit.SECONDS.between(birthDate,today));
    System.out.println("相差的毫秒数"+ ChronoUnit.MILLIS.between(birthDate,today));
    System.out.println("相差的微秒数"+ ChronoUnit.MICROS.between(birthDate,today));
    System.out.println("相差的纳秒数"+ ChronoUnit.NANOS.between(birthDate,today));
    System.out.println("相差的半天数"+ ChronoUnit.HALF_DAYS.between(birthDate,today));
    System.out.println("相差的十年数"+ ChronoUnit.DECADES.between(birthDate,today));
    System.out.println("相差的世纪(百年)数"+ ChronoUnit.CENTURIES.between(birthDate,today));
    System.out.println("相差的千年数"+ ChronoUnit.MILLENNIA.between(birthDate,today));
    System.out.println("相差的纪元数"+ ChronoUnit.ERAS.between(birthDate,today));
}
相差的年数23
相差的月数282
相差的周数1226
相差的天数8587
相差的时数206095
相差的分数12365717
相差的秒数741943055
相差的毫秒数741943055971
相差的微秒数741943055971000
相差的纳秒数741943055971000000
相差的半天数17174
相差的十年数2
相差的世纪(百年)数0
相差的千年数0
相差的纪元数0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值