Java8 时间工具类 您所在的位置:网站首页 localdatetime毫秒数 Java8 时间工具类

Java8 时间工具类

2023-04-28 00:53| 来源: 网络整理| 查看: 265

Java8 时间工具类 原创

wx5ed88c086e740 2023-04-21 00:18:12 博主文章分类:---- 各种模板 ----- ©著作权

文章标签 字符串 java 时间格式 文章分类 Html/CSS 前端开发

©著作权归作者所有:来自51CTO博客作者wx5ed88c086e740的原创作品,请联系作者获取转载授权,否则将追究法律责任

import lombok.Getter; import java.time.*; import java.time.format.DateTimeFormatter; import java.time.temporal.TemporalAdjusters; import java.util.Date; /** * 时间工具类 * * @Author: taozhengzhi * @Date: 2019/4/4 上午10:10 * @Version 1.0 */ public class DateUntils { /** * 获取当前日期 格式为 (xxxx-xx-xx) * * @return */ public static LocalDate getCurrentDate() { return LocalDate.now(); } /** * 获取当前时间 格式为 (xxxx-xx-xx xx:xx:xx.xxx) * * @return */ public static LocalDateTime getLocalDateTime() { return LocalDateTime.now(); } /** * Date 转化为 格式化时间 * * @param date 待转化的date * @param pattern 时间格式 * @return */ public static String format(Date date, DateTimeFormatterEnum pattern) { return format(LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()), pattern); } /** * LocalDateTime 转换为格式化时间 * * @param localDateTime 待转化的localDateTime * @param pattern 时间格式 * @return */ public static String format(LocalDateTime localDateTime, DateTimeFormatterEnum pattern) { return localDateTime.format(pattern.formatter); } /** * 字符串转为格式化Date * * @param date 时间字符串 * @param pattern 时间格式 * @return */ public static Date parseDate(String date, DateTimeFormatterEnum pattern) { return Date.from(parseLocalDateTime(date, pattern).atZone(ZoneId.systemDefault()).toInstant()); } /** * 字符串转格式化LocalDateTime * * @param date 时间字符串 * @param pattern 时间格式 * @return */ public static LocalDateTime parseLocalDateTime(String date, DateTimeFormatterEnum pattern) { return LocalDateTime.parse(date, pattern.formatter); } /** * 今天的开始 * * @return */ public static Date todayBegin() { return Date.from(LocalDateTime.of(getCurrentDate(), LocalTime.MIN).atZone(ZoneId.systemDefault()).toInstant()); } /** * 今天的结束 * * @return */ public static Date todayEnd() { return Date.from(LocalDateTime.of(getCurrentDate(), LocalTime.MAX).atZone(ZoneId.systemDefault()).toInstant()); } /** * 昨天的开始 * * @return */ public static Date yesterdayBegin() { return Date.from(LocalDateTime.of(getCurrentDate().plusDays(-1L), LocalTime.MIN).atZone(ZoneId.systemDefault()).toInstant()); } /** * 昨天的结束 * * @return */ public static Date yesterdayEnd() { return Date.from(LocalDateTime.of(getCurrentDate().plusDays(-1L), LocalTime.MAX).atZone(ZoneId.systemDefault()).toInstant()); } /** * 明天的开始 * * @return */ public static Date tomorrowBegin() { return Date.from(LocalDateTime.of(getCurrentDate().plusDays(1L), LocalTime.MIN).atZone(ZoneId.systemDefault()).toInstant()); } /** * 明天的结束 * * @return */ public static Date tomorrowEnd() { return Date.from(LocalDateTime.of(getCurrentDate().plusDays(1L), LocalTime.MAX).atZone(ZoneId.systemDefault()).toInstant()); } /** * 指定天的开始 * * @param date 指定日期 * @return */ public static Date startTime(Date date) { return Date.from(LocalDateTime.of(LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).toLocalDate(), LocalTime.MIN).atZone(ZoneId.systemDefault()).toInstant()); } /** * 指定天的结束 * * @param date * @return */ public static Date endTime(Date date) { return Date.from(LocalDateTime.of(LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).toLocalDate(), LocalTime.MAX).atZone(ZoneId.systemDefault()).toInstant()); } /** * 当月的第一天 * * @return */ public static Date monthBegin() { return Date.from(LocalDateTime.of(getCurrentDate().with(TemporalAdjusters.firstDayOfMonth()), LocalTime.MIN).atZone(ZoneId.systemDefault()).toInstant()); } /** * 当月的最后一天 * * @return */ public static Date monthEnd() { return Date.from(LocalDateTime.of(getCurrentDate().with(TemporalAdjusters.lastDayOfMonth()), LocalTime.MAX).atZone(ZoneId.systemDefault()).toInstant()); } /** * 指定月的第一天 * * @param date * @return */ public static Date theMonthBegin(Date date) { return Date.from(LocalDateTime.of(LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).with(TemporalAdjusters.firstDayOfMonth()).toLocalDate(), LocalTime.MIN).atZone(ZoneId.systemDefault()).toInstant()); } /** * 指定月的最后一天 * * @param date * @return */ public static Date theMonthEnd(Date date) { return Date.from(LocalDateTime.of(LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).with(TemporalAdjusters.lastDayOfMonth()).toLocalDate(), LocalTime.MAX).atZone(ZoneId.systemDefault()).toInstant()); } /** * 指定年的第一天 * * @param date * @return */ public static Date theYearBegin(Date date) { return Date.from(LocalDateTime.of(LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).with(TemporalAdjusters.firstDayOfYear()).toLocalDate(), LocalTime.MIN).atZone(ZoneId.systemDefault()).toInstant()); } /** * 指定年的第一天 * * @param date * @return */ public static Date theYearEnd(Date date) { return Date.from(LocalDateTime.of(LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).with(TemporalAdjusters.lastDayOfYear()).toLocalDate(), LocalTime.MAX).atZone(ZoneId.systemDefault()).toInstant()); } /** * 判断传入时间是否是今天 * * @param date * @return */ public static Boolean checkIsToday(Date date) { return getCurrentDate().equals(LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).toLocalDate()); } /** * 计算一段时间的间隔 * duration.toDays() 计算这段时间的总天数(满24小时为1天) * duration.toHours() 计算这段时间的总小时数(满60分钟为1小时) * duration.toMinutes() 计算这段时间的总分钟数(满60秒为1分钟) * duration.getSeconds() 计算这段时间的总秒数(满1000毫秒为1秒) * duration.toMillis() 计算这段时间的总毫秒数 * duration.toNanos() 计算这段时间的总纳秒数 * * @param beginTime 开始时间 * @param endTime 结束时间 * @return */ public static Duration between(Date beginTime, Date endTime) { LocalDateTime beginLocalDateTime = LocalDateTime.ofInstant(beginTime.toInstant(), ZoneId.systemDefault()); LocalDateTime endLocalDateTime = LocalDateTime.ofInstant(endTime.toInstant(), ZoneId.systemDefault()); return Duration.between(beginLocalDateTime, endLocalDateTime); } /** * 指定日期的几天前 * @param date 指定日期 * @param num 向前的天数 * @return */ public static Date theDayBefor(Date date, long num) { return Date.from(LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).minusDays(num).atZone(ZoneId.systemDefault()).toInstant()); } /** * 指定日期的几天后 * @param date 指定日期 * @param num 向后的天数 * @return */ public static Date theDayLater(Date date, long num){ return Date.from(LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).plusDays(num).atZone(ZoneId.systemDefault()).toInstant()); } /** * 日期格式枚举(如果有需要自己加) */ public enum DateTimeFormatterEnum { /** * yyyy-MM-dd HH:mm:ss */ FORMAT_DATE_TIME_STYLE_1("yyyy-MM-dd HH:mm:ss", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); @Getter private String value; @Getter private DateTimeFormatter formatter; DateTimeFormatterEnum(String value, DateTimeFormatter formatter) { this.value = value; this.formatter = formatter; } } }

 

收藏 评论 分享 举报

上一篇:JAVA编程思想学习 --- 第三章(控制程序流程)

下一篇:JAVA编程思想学习 --- 第五章 (隐藏实施过程)



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有