Java8新的时间和日期API LocalDateTime等使用

前言

在之前版本的时间处理上,存在各式各样的缺陷或不易于使用1
但是在实际的开发中,Date类、SimpleDateFormat类仍然在使用,既然官方都给出了新的替代品,为什么不学习使用呢?
LocalDateTime等位于java.time包下,专门用于处理时间2
在这里插入图片描述
挑一些常用的来学习一下:

获取当前时间:
public static LocalDateTime now()
从默认时区的系统时钟获取当前的日期时间。
返回值:
当前日期时间使用系统时钟和默认时区,不为空

使用:

		LocalDateTime localDateTime = LocalDateTime.now();
		System.out.println("获取当前时间:"+localDateTime);
		//output
		//获取当前时间:2021-01-12T17:30:24.181
时间格式化 DateTimeFormatter
主要的日期时间类提供两种方法 - 一种用于格式化, format(DateTimeFormatter formatter) ,
 format(DateTimeFormatter formatter)一种用于解析, parse(CharSequence text, DateTimeFormatter formatter) 。

使用:

		LocalDateTime localDateTime = LocalDateTime.now();
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		System.out.println("格式化日期:"+localDateTime.format(formatter));
		//或
		System.out.println("格式化日期:"+formatter.format(localDateTime));


		//格式化日期:2021-01-12 17:42:13
		//格式化日期:2021-01-12 17:42:13
获取当前时间戳(毫秒)

这里使用Instant类,可以用他获取某一时间点的时间戳。
使用:

		LocalDateTime localDateTime = LocalDateTime.now();
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		System.out.println("时间戳:"+localDateTime.toInstant(ZoneOffset.of("+0")).toEpochMilli());//1
		//时间戳:1610475251753
		System.out.println("时间戳:"+localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli());//2
		//时间戳:1610446451753
		System.out.println("时间戳:"+localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli());//3
		//时间戳:1610446451753
		System.out.println("时间戳:"+Instant.now().toEpochMilli());//4
		//时间戳:1610446451761
		System.out.println("时间戳:"+System.currentTimeMillis());
		//时间戳:1610446451761

使用LocalDateTime转换时需要设置时区,可以看到,第一种设的0,而我所在的是东八区,所以第二第三两种形势加了八小时,也可以直接使用Instant.now()直接转化(四),最后是系统时间戳。

时间戳转时间

同样需要使用Instant类进行处理。
使用:

		LocalDateTime localDateTime = LocalDateTime.now();
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		System.out.println("时间戳转时间:"+Instant.ofEpochMilli(System.currentTimeMillis()).
		atZone(ZoneOffset.ofHours(8)).format(formatter));
		//时间戳转时间:2021-01-12 18:43:36
	
时间操作

对时间进行加减操作,对于特殊的时间,比如03-01可以自动处理为02-28

		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		LocalDateTime localDateTime = LocalDateTime.parse("2021-03-01 18:56:48",formatter);
		System.out.println("格式化日期:"+formatter.format(localDateTime));
		//格式化日期:2021-03-01 18:56:48
		System.out.println("格式化日期 减一天:"+formatter.format(localDateTime.minusDays(1)));
		//格式化日期 减一天:2021-02-28 18:56:48
		System.out.println("格式化日期 减一月:"+formatter.format(localDateTime.minusMonths(1)));
		//格式化日期 减一月:2021-02-01 18:56:48

此外还有with相关的调整,对时间天数、月份、年等进行调整,如果调整的数值不在自然区间内,如二月调整到30号,会抛出DateTimeException异常。

暂时就这么多吧。


  1. 为什么不建议使用Date,而是使用Java8新的时间和日期API? ↩︎

  2. Java 8 中文文档 ↩︎

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值