JDK8 时间处理

将毫秒转为时分秒

IInstant.ofEpochmilli(毫秒).atZone(ZoneId.of("Asia/Shanghai")).toLocalDateTime();  //完整日期,国内时区 Shanghai

IInstant.ofEpochmilli(毫秒).atZone(ZoneId.of("Asia/Shanghai")).toLocalDateTime().replace("T", " ").subString(11); //时分秒

判断当前时间在哪一个时间段

(LocalTime.parse(时:分:秒).isAfter(LocalTime(时:分:秒)  && LocalTime.parse(时:分:秒).isBefor(LocalTime(时:分:秒) )

LocalTime.parse(当前时间).isAfter(LocalTime(时:分:秒) &&  LocalTime.parse(当前时间).isBefor(LocalTime(时:分:秒) 

LocalTime.parse(“7:20:01”).isAfter(LocalTime("7:20:00") &&  LocalTime.parse(“7:20:01”).isBefor(LocalTime("7:21:00") 

上面的意思是: 7:20:01 是否在7:20:00 之后  并且  7:20:01  在 7:21:00 之前  ,如果满足条件则在此时间段之内。



判断当前时间是  当天的 十二点之前  还是当天的十二点之后

LocalTime.parse(时分秒).getHour() - 12 > 0   当前时 减12 ,  如果大于0   则 是 当天了。  如果小于0  则是第二天了





java将date 毫秒转为日期

LocalDateTime.ofInstant(date.toInstant(), ZoneId.of('Asia/Shanghai')).format(DateTimeFormatter.ofPattern('yyyy-mm-dd HH:mm:ss'));

根据字符串时间  进行加减

LocalDateTime.parse(StringDate,DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")).minusYears(1);

这个是根据字符串时间减一年。



将 Date  转化为  LocalDateTime

LocalDateTime local = LocalDateTime.ofInstant(  (Date) .toInstant, ZoneId.of('Asia/Shanghai'));

将LocalDateTime 转为 Date

Date date = Date.from(LocalDateTime.now().atZone(ZoneId.of("Asia/Shanghai")).toInstant());

根据字符串 加减时间

kssj = LocalDateTime.parse(字符串时间, DateTimeFormatter.ofpattern("yyyy-MM-dd HH:mm:ss")).minus(1, ChronoUnit.DAYS).format("yyyy-MM-dd HH:mmss");  这个是在字符串时间的基础上减一天。 加是:plus. 将minus改为plus



获取当前时间  以指定的 格式

LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss"))

在当前时间减去指定的分钟后格式化时间

当前时间减五分钟后格式化

LocalDateTime.now().minus(5,ChronoUnit.MINUTES).format(DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss"))

将字符串转为时间戳(毫秒)

String time = "2018-01-07 15:20:23";

long milli = LocalDateTime.parse(time,DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").toInstant(ZoneOffset.ofHours(0)).toEpochMilli())

system.out.pringln(milli );  值是:1515309623000

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值