Java8中LocalDateTime与时间戳timestamp的互相转换
Java8LocalDateTime与timestamp转换
将timestamp转为LocalDateTime
publicLocalDateTimetimestamToDatetime(longtimestamp){ Instantinstant=Instant.ofEpochMilli(timestamp); returnLocalDateTime.ofInstant(instant,ZoneId.systemDefault()); }
将LocalDataTime转为timestamp
publiclongdatatimeToTimestamp(LocalDateTimeldt){ longtimestamp=ldt.toInstant(ZoneOffset.of("+8")).toEpochMilli(); returntimestamp; }
我在网上还找到了另一个将datetime转为时间戳的方法:
ZoneIdzone=ZoneId.systemDefault(); longtimestamp=ldt.atZone(zone).toInstant().toEpochMilli();
Java8的时间转为时间戳的大概的思路就是LocalDateTime先转为Instant,设置时区,然后转timestamp。
附一个Java8中的LocalDateTime工具类
工具类
packagecom.kingboy.common.utils.date; importjava.time.*; importjava.time.format.DateTimeFormatter; importjava.time.temporal.ChronoUnit; importjava.time.temporal.TemporalUnit; importjava.util.Date; /* *@authorkingboy *@Date2017/7/22下午2:12 *@DescriptionLocalDateTimeUtilsisusedtoJava8中的时间类 */ publicclassLocalDateTimeUtils{ //获取当前时间的LocalDateTime对象 //LocalDateTime.now(); //根据年月日构建LocalDateTime //LocalDateTime.of(); //比较日期先后 //LocalDateTime.now().isBefore(), //LocalDateTime.now().isAfter(), //Date转换为LocalDateTime publicstaticLocalDateTimeconvertDateToLDT(Datedate){ returnLocalDateTime.ofInstant(date.toInstant(),ZoneId.systemDefault()); } //LocalDateTime转换为Date publicstaticDateconvertLDTToDate(LocalDateTimetime){ returnDate.from(time.atZone(ZoneId.systemDefault()).toInstant()); } //获取指定日期的毫秒 publicstaticLonggetMilliByTime(LocalDateTimetime){ returntime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); } //获取指定日期的秒 publicstaticLonggetSecondsByTime(LocalDateTimetime){ returntime.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond(); } //获取指定时间的指定格式 publicstaticStringformatTime(LocalDateTimetime,Stringpattern){ returntime.format(DateTimeFormatter.ofPattern(pattern)); } //获取当前时间的指定格式 publicstaticStringformatNow(Stringpattern){ returnformatTime(LocalDateTime.now(),pattern); } //日期加上一个数,根据field不同加不同值,field为ChronoUnit.* publicstaticLocalDateTimeplus(LocalDateTimetime,longnumber,TemporalUnitfield){ returntime.plus(number,field); } //日期减去一个数,根据field不同减不同值,field参数为ChronoUnit.* publicstaticLocalDateTimeminu(LocalDateTimetime,longnumber,TemporalUnitfield){ returntime.minus(number,field); } /** *获取两个日期的差field参数为ChronoUnit.* *@paramstartTime *@paramendTime *@paramfield单位(年月日时分秒) *@return */ publicstaticlongbetweenTwoTime(LocalDateTimestartTime,LocalDateTimeendTime,ChronoUnitfield){ Periodperiod=Period.between(LocalDate.from(startTime),LocalDate.from(endTime)); if(field==ChronoUnit.YEARS)returnperiod.getYears(); if(field==ChronoUnit.MONTHS)returnperiod.getYears()*12+period.getMonths(); returnfield.between(startTime,endTime); } //获取一天的开始时间,2017,7,2200:00 publicstaticLocalDateTimegetDayStart(LocalDateTimetime){ returntime.withHour(0) .withMinute(0) .withSecond(0) .withNano(0); } //获取一天的结束时间,2017,7,2223:59:59.999999999 publicstaticLocalDateTimegetDayEnd(LocalDateTimetime){ returntime.withHour(23) .withMinute(59) .withSecond(59) .withNano(999999999); } }
测试类
packagecom.kingboy.common.localdatetimeutils; importcom.kingboy.common.utils.date.LocalDateTimeUtils; importorg.junit.Test; importjava.time.LocalDateTime; importjava.time.temporal.ChronoUnit; importstaticcom.kingboy.common.utils.date.LocalDateTimeUtils.getDayEnd; importstaticcom.kingboy.common.utils.date.LocalDateTimeUtils.getDayStart; /** *@authorkingboy *@Date2017/7/22下午7:16 *@DescriptionLocaDateTimeUtilsTestisusedto测试LocalDateTime工具 */ publicclassLocaDateTimeUtilsTest{ @Test publicvoidformat_test(){ System.out.println(LocalDateTimeUtils.formatNow("yyyy年MM月dd日HH:mm:ss")); } @Test publicvoidbetweenTwoTime_test(){ LocalDateTimestart=LocalDateTime.of(1993,10,13,11,11); LocalDateTimeend=LocalDateTime.of(1994,11,13,13,13); System.out.println("年:"+LocalDateTimeUtils.betweenTwoTime(start,end,ChronoUnit.YEARS)); System.out.println("月:"+LocalDateTimeUtils.betweenTwoTime(start,end,ChronoUnit.MONTHS)); System.out.println("日:"+LocalDateTimeUtils.betweenTwoTime(start,end,ChronoUnit.DAYS)); System.out.println("半日:"+LocalDateTimeUtils.betweenTwoTime(start,end,ChronoUnit.HALF_DAYS)); System.out.println("小时:"+LocalDateTimeUtils.betweenTwoTime(start,end,ChronoUnit.HOURS)); System.out.println("分钟:"+LocalDateTimeUtils.betweenTwoTime(start,end,ChronoUnit.MINUTES)); System.out.println("秒:"+LocalDateTimeUtils.betweenTwoTime(start,end,ChronoUnit.SECONDS)); System.out.println("毫秒:"+LocalDateTimeUtils.betweenTwoTime(start,end,ChronoUnit.MILLIS)); //============================================================================================= /* 年:1 月:13 日:396 半日:792 小时:9506 分钟:570362 秒:34221720 毫秒:34221720000 */ } @Test publicvoidplus_test(){ //增加二十分钟 System.out.println(LocalDateTimeUtils.formatTime(LocalDateTimeUtils.plus(LocalDateTime.now(), 20, ChronoUnit.MINUTES),"yyyy年MM月dd日HH:mm")); //增加两年 System.out.println(LocalDateTimeUtils.formatTime(LocalDateTimeUtils.plus(LocalDateTime.now(), 2, ChronoUnit.YEARS),"yyyy年MM月dd日HH:mm")); //============================================================================================= /* 2017年07月22日22:53 2019年07月22日22:33 */ } @Test publicvoiddayStart_test(){ System.out.println(getDayStart(LocalDateTime.now())); System.out.println(getDayEnd(LocalDateTime.now())); //============================================================================================= /* 2017-07-22T00:00 2017-07-22T23:59:59.999999999 */ } }
总结
到此这篇关于Java8中LocalDateTime与时间戳timestamp互相转换的文章就介绍到这了,更多相关Java8LocalDateTime与timestamp转换内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。