詳解Java中日期工具類的操作
避免重復(fù)造輪子,相關(guān)方法基于hutool日期時(shí)間工具封裝并做部分增強(qiáng)。需要先引入如下坐標(biāo)
<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>${hutool.version}</version> </dependency>
字符串轉(zhuǎn)Date
//字符串轉(zhuǎn)Date Date dateTime = DateUtil.parseDate("2022-04-06"); System.out.println(dateTime);
Date轉(zhuǎn)字符串
//Date轉(zhuǎn)字符串不指定format格式,默認(rèn)yyyy-MM-dd HH:mm:ss String dateStr = DateUtil.date2Str(new Date()); System.out.println(dateStr); //Date轉(zhuǎn)字符串指定格式 String dateStr2 = DateUtil.date2Str("yyyy/MM/dd",new Date()); System.out.println(dateStr2);
字符串轉(zhuǎn)LocalDate
//字符串轉(zhuǎn)LocalDate LocalDate localDate = DateUtil.parseLocalDate("2022-04-06"); System.out.println(localDate);
Date轉(zhuǎn)LocalDate
//Date轉(zhuǎn)LocalDate LocalDate localDate = DateUtil.date2LocalDate(new Date()); System.out.println(localDate);
LocalDate轉(zhuǎn)字符串
//LocalDate轉(zhuǎn)Str String localDateStr = DateUtil.localDate2Str(LocalDate.now()); System.out.println(localDateStr);
兩個(gè)日期的時(shí)間差
String beginDateStr = "2022-02-01 22:33:23"; Date beginDate = DateUtil.parse(beginDateStr); String endDateStr = "2022-03-10 23:33:23"; Date endDate = DateUtil.parse(endDateStr); //相差天數(shù)(37) long betweenDay = DateUtil.between(beginDate, endDate, DateUnit.DAY); System.out.println(betweenDay); //格式化時(shí)間差(37天1小時(shí)) String formatBetween = DateUtil.formatBetween(beginDate, endDate, BetweenFormater.Level.HOUR); System.out.println(formatBetween);
一天的開始和結(jié)束時(shí)間
String dateStr = "2022-04-07 10:33:23"; Date date = DateUtil.parse(dateStr); //一天的開始時(shí)間:2022-04-07 00:00:00 Date beginOfDay = DateUtil.beginOfDay(date); System.out.println(beginOfDay); //一天的結(jié)束時(shí)間:2022-04-07 23:59:59 Date endOfDay = DateUtil.endOfDay(date); System.out.println(endOfDay);
工具類如下
/** * <p>基于hutool的日期工具類增強(qiáng)</p> * * @author zjq * @date 2021/04/07 */ public class DateUtil extends cn.hutool.core.date.DateUtil { private static final String[] PARSE_PATTERNS = { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"}; public static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss"); public static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd"); public static final DateTimeFormatter DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); /** * 字符串轉(zhuǎn)date類型 * * @param dateStr * @return */ public static Date parseDate(Object dateStr) { if (ObjectUtils.isNull(dateStr)) { return null; } if (dateStr instanceof Double) { return org.apache.poi.ss.usermodel.DateUtil.getJavaDate((Double) dateStr); } return parse(dateStr.toString(), PARSE_PATTERNS); } /** * Date類型轉(zhuǎn)字符串 * * @param date * @return yyyy-MM-dd HH:mm:ss */ public static String date2Str(Date date) { return date2Str(null, date); } /** * Date類型轉(zhuǎn)字符串 * * @param format * @param date * @return */ public static String date2Str(String format, Date date) { if (ObjectUtils.isNull(date)) { return null; } SimpleDateFormat dateFormat = StrUtil.isNotBlank(format) ?new SimpleDateFormat(format): new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return dateFormat.format(date); } /** * 字符串轉(zhuǎn)LocalTime類型 * * @param dateStr * @return */ public static LocalTime parseLocalTime(Object dateStr) { if (ObjectUtils.isNull(dateStr)) { return null; } if (dateStr instanceof Double) { return date2LocalTime(parseDate(dateStr)); } return LocalTime.parse(dateStr.toString(), TIME_FORMATTER); } /** * Date轉(zhuǎn)LocalTime * * @param date * @return */ public static LocalTime date2LocalTime(Date date) { if (null == date) { return null; } return date.toInstant().atZone(ZoneId.systemDefault()).toLocalTime(); } /** * LocalTime轉(zhuǎn)Str * * @param localTime * @return HH:mm:ss */ public static String localTime2Str(LocalTime localTime) { return localTime2Str(null, localTime); } /** * LocalTime轉(zhuǎn)str * * @param format 格式 * @param localTime * @return */ public static String localTime2Str(String format, LocalTime localTime) { if (null == localTime) { return null; } DateTimeFormatter formatter = StrUtil.isBlank(format) ? TIME_FORMATTER : DateTimeFormatter.ofPattern(format); return localTime.format(formatter); } /** * 字符串轉(zhuǎn)LocalDate類型 * * @param dateStr * @return */ public static LocalDate parseLocalDate(Object dateStr) { if (ObjectUtils.isNull(dateStr)) { return null; } if (dateStr instanceof Double) { return date2LocalDate(parseDate(dateStr)); } return LocalDate.parse(dateStr.toString(), DATE_FORMATTER); } /** * Date轉(zhuǎn)LocalDate * * @param date * @return */ public static LocalDate date2LocalDate(Date date) { if (null == date) { return null; } return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); } /** * LocalDate轉(zhuǎn)Str * * @param localDate * @return */ public static String localDate2Str(LocalDate localDate) { return localDate2Str(null, localDate); } /** * LocalDate轉(zhuǎn)Str * * @param format 格式 * @param localDate * @return */ public static String localDate2Str(String format, LocalDate localDate) { if (null == localDate) { return null; } DateTimeFormatter formatter = StrUtil.isBlank(format) ? DATE_FORMATTER : DateTimeFormatter.ofPattern(format); return localDate.format(formatter); } /** * 字符串轉(zhuǎn)LocalDateTime類型 * * @param dateStr * @return */ public static LocalDateTime parseLocalDateTime(Object dateStr) { if (ObjectUtils.isNull(dateStr)) { return null; } if (dateStr instanceof Double) { return date2LocalDateTime(parseDate(dateStr)); } return LocalDateTime.parse(dateStr.toString(), DATETIME_FORMATTER); } /** * Date轉(zhuǎn)LocalDateTime * * @param date * @return */ public static LocalDateTime date2LocalDateTime(Date date) { if (null == date) { return null; } return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); } /** * LocalDate轉(zhuǎn)Str * * @param localDateTime * @return */ public static String localDateTime2Str(LocalDateTime localDateTime) { return localDateTime2Str(null, localDateTime); } /** * LocalDate轉(zhuǎn)Str * * @param format * @param localDateTime * @return */ public static String localDateTime2Str(String format, LocalDateTime localDateTime) { if (null == localDateTime) { return null; } DateTimeFormatter formatter = StrUtil.isBlank(format) ? DATETIME_FORMATTER : DateTimeFormatter.ofPattern(format); return localDateTime.format(formatter); } }
到此這篇關(guān)于詳解Java中日期工具類的操作的文章就介紹到這了,更多相關(guān)Java日期工具類內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
十大常見Java String問題_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
本文介紹Java中關(guān)于String最常見的10個(gè)問題,需要的朋友參考下吧2017-04-04解決JAVA項(xiàng)目啟動(dòng)卡住,無任何異常信息的問題
這篇文章主要介紹了解決JAVA項(xiàng)目啟動(dòng)卡住,無任何異常信息的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03springboot運(yùn)行jar生成的日志到指定文件進(jìn)行管理方式
這篇文章主要介紹了springboot運(yùn)行jar生成的日志到指定文件進(jìn)行管理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04Spring Boot的應(yīng)用啟動(dòng)與關(guān)閉的方法
本篇文章主要介紹了Spring Boot的應(yīng)用啟動(dòng)與關(guān)閉的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-12Spring Cache與Redis結(jié)合的使用方式
這篇文章主要介紹了Spring Cache與Redis結(jié)合的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12java-jsp springmvc-controller 傳值到頁面的方法
下面小編就為大家分享一篇java-jsp springmvc-controller 傳值到頁面的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-03-03詳解Spring Boot加載properties和yml配置文件
本篇文章主要介紹了詳解Spring Boot加載properties和yml配置文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04SpringBoot 如何實(shí)現(xiàn)自定義Redis序列化
這篇文章主要介紹了SpringBoot 如何實(shí)現(xiàn)自定義Redis序列化方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10