Java8中的LocalDateTime工具类

Wesley13
• 阅读 861

网上搜索了半天都没有找到Java8的LocalDateTime的工具类,只好自己写了一个,常用功能基本都有。还在用Date的Java同道该换换了

个人项目地址:https://github.com/KingBoyWorld/common.git,Common模块中有很多实用的工具包,都是优化过的。

工具类

package com.kingboy.common.utils.date; import java.time.\*; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; import java.time.temporal.TemporalUnit; import java.util.Date; /\* \* @author kingboy \* @Date 2017/7/22 下午2:12 \* @Description LocalDateTimeUtils is used to Java8中的时间类 \*/ public class LocalDateTimeUtils { //获取当前时间的LocalDateTime对象 //LocalDateTime.now(); //根据年月日构建LocalDateTime //LocalDateTime.of(); //比较日期先后 //LocalDateTime.now().isBefore(), //LocalDateTime.now().isAfter(), //Date转换为LocalDateTime public static LocalDateTime convertDateToLDT(Date date) { return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()); } //LocalDateTime转换为Date public static Date convertLDTToDate(LocalDateTime time) { return Date.from(time.atZone(ZoneId.systemDefault()).toInstant()); } //获取指定日期的毫秒 public static Long getMilliByTime(LocalDateTime time) { return time.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); } //获取指定日期的秒 public static Long getSecondsByTime(LocalDateTime time) { return time.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond(); } //获取指定时间的指定格式 public static String formatTime(LocalDateTime time,String pattern) { return time.format(DateTimeFormatter.ofPattern(pattern)); } //获取当前时间的指定格式 public static String formatNow(String pattern) { return formatTime(LocalDateTime.now(), pattern); } //日期加上一个数,根据field不同加不同值,field为ChronoUnit.\* public static LocalDateTime plus(LocalDateTime time, long number, TemporalUnit field) { return time.plus(number, field); } //日期减去一个数,根据field不同减不同值,field参数为ChronoUnit.\* public static LocalDateTime minu(LocalDateTime time, long number, TemporalUnit field){ return time.minus(number,field); } /\*\* \* 获取两个日期的差 field参数为ChronoUnit.\* \* @param startTime \* @param endTime \* @param field 单位(年月日时分秒) \* @return \*/ public static long betweenTwoTime(LocalDateTime startTime, LocalDateTime endTime, ChronoUnit field) { Period period = Period.between(LocalDate.from(startTime), LocalDate.from(endTime)); if (field == ChronoUnit.YEARS) return period.getYears(); if (field == ChronoUnit.MONTHS) return period.getYears() \* 12 + period.getMonths(); return field.between(startTime, endTime); } //获取一天的开始时间,2017,7,22 00:00 public static LocalDateTime getDayStart(LocalDateTime time) { return time.withHour(0) .withMinute(0) .withSecond(0) .withNano(0); } //获取一天的结束时间,2017,7,22 23:59:59.999999999 public static LocalDateTime getDayEnd(LocalDateTime time) { return time.withHour(23) .withMinute(59) .withSecond(59) .withNano(999999999); } } 

测试类

package com.kingboy.common.localdatetimeutils; import com.kingboy.common.utils.date.LocalDateTimeUtils; import org.junit.Test; import java.time.LocalDateTime; import java.time.temporal.ChronoUnit; import static com.kingboy.common.utils.date.LocalDateTimeUtils.getDayEnd; import static com.kingboy.common.utils.date.LocalDateTimeUtils.getDayStart; /\*\* \* @author kingboy \* @Date 2017/7/22 下午7:16 \* @Description LocaDateTimeUtilsTest is used to 测试LocalDateTime工具 \*/ public class LocaDateTimeUtilsTest { @Test public void format\_test() { System.out.println(LocalDateTimeUtils.formatNow("yyyy年MM月dd日 HH:mm:ss")); } @Test public void betweenTwoTime\_test() { LocalDateTime start = LocalDateTime.of(1993, 10, 13, 11, 11); LocalDateTime end = 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 public void plus\_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 public void dayStart\_test() { System.out.println(getDayStart(LocalDateTime.now())); System.out.println(getDayEnd(LocalDateTime.now())); //============================================================================================= /\* 2017-07-22T00:00 2017-07-22T23:59:59.999999999 \*/ } } 
原文地址:https://blog.csdn.net/kingboyworld/article/details/75808108
点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Wesley13 Wesley13
2年前
java反射, 不看你可别后悔
<divid"content\_views"class"markdown\_views"<!flowchart箭头图标勿删<svgxmlns"http://www.w3.org/2000/svg"style"display:none;"<pathstrokelinecap"round"d"M5,00,
Stella981 Stella981
2年前
AndroidStudio封装SDK的那些事
<divclass"markdown\_views"<!flowchart箭头图标勿删<svgxmlns"http://www.w3.org/2000/svg"style"display:none;"<pathstrokelinecap"round"d"M5,00,2.55,5z"id"raphael
Stella981 Stella981
2年前
Native memory allocation (mmap) failed to map xxx bytes for committing reserved memory
<divid"content\_views"class"markdown\_views"<!flowchart箭头图标勿删<svgxmlns"http://www.w3.org/2000/svg"style"display:none;"<pathstrokelinecap"round"d"M5,00,
Stella981 Stella981
2年前
Spring Boot 2下使用Feign找不到@EnableFeignClients的解决办法
<divid"content\_views"class"markdown\_views"<!flowchart箭头图标勿删<svgxmlns"http://www.w3.org/2000/svg"style"display:none;"<pathstrokelinecap"round"d"M5,00,2
Stella981 Stella981
2年前
Jenkins发送邮件
<divid"content\_views"class"markdown\_viewsprismatomonedark"<!flowchart箭头图标勿删<svgxmlns"http://www.w3.org/2000/svg"style"display:none;"<pathstrokelineca
Stella981 Stella981
2年前
Android So动态加载 优雅实现与原理分析
背景:漫品Android客户端集成适配转换功能(基于目标识别(So库35M)和人脸识别库(5M)),导致apk体积50M左右,为优化客户端体验,决定实现So文件动态加载.!(https://oscimg.oschina.net/oscnet/00d1ff90e4b34869664fef59e3ec3fdd20b.png)点击上方“蓝字”关注我
Stella981 Stella981
2年前
CentOS7开启docker远程访问
<divid"content\_views"class"markdown\_viewsprismdracula"<!flowchart箭头图标勿删<svgxmlns"http://www.w3.org/2000/svg"style"display:none;"<pathstrokelinecap"rou
Stella981 Stella981
2年前
Neo4j
<divid"content\_views"class"markdown\_viewsprismgithubgist"<!flowchart箭头图标勿删<svgxmlns"http://www.w3.org/2000/svg"style"display:none;"<pathstrokelinecap
Stella981 Stella981
2年前
SpringBoot 使用yml配置 mybatis+pagehelper+druid+freemarker实例
<divid"article\_content"class"article\_contenttrackingad"datamod"popu\_307"datadsm"post"style"overflow:hidden;"<divclass"markdown\_views"<h2id"springboot使用ym