您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“JAVA Date時(shí)間工具類的實(shí)例用法”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“JAVA Date時(shí)間工具類的實(shí)例用法”吧!
import java.text.ParseException; import java.time.*; import java.time.format.DateTimeFormatter; import java.time.temporal.TemporalAdjusters; import java.util.Date; /** * @description: 時(shí)間工具類 * @author: kejie.huang * @date: Created in 2019/10/10 11:08 * @version: * @modified By: */ public class DateUtils { public static final String DATE_FORMART_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss"; /** * 指定日期加上天數(shù)后的日期 * @param num 為增加的天數(shù) * @param currentDate 創(chuàng)建時(shí)間 * @return * @throws ParseException */ public static Date plusDay(int num, Date currentDate) { LocalDateTime localDateTime = dateConvertLocalDateTime(currentDate); localDateTime = localDateTime.plusDays(num); return localDateTimeConvertDate(localDateTime); } /** * @description 根據(jù)時(shí)間字符串轉(zhuǎn)換成 * @return * @author kejie.huang * @date 2019/10/10 11:19 */ public static Date dateStrConvertDate(String currentDate, String formateStr) { DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(formateStr); LocalDateTime localDateTime = LocalDateTime.parse(currentDate, dateTimeFormatter); return localDateTimeConvertDate(localDateTime); } /** * @return * @description 根據(jù)date轉(zhuǎn)換成localDateTime * @author kejie.huang * @date 2019/10/14 14:31 */ private static LocalDateTime dateConvertLocalDateTime(Date date) { LocalDateTime localDateTime = Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime(); return localDateTime; } /** * @return * @description 方法描述 根據(jù)localDateTime轉(zhuǎn)換成date * @author kejie.huang * @date 2019/10/14 14:31 */ public static Date localDateTimeConvertDate(LocalDateTime localDateTime) { return Date.from(getZonedDateTimeByLocalDateTime(localDateTime).toInstant()); } /** * @description 根據(jù)時(shí)間轉(zhuǎn)換成時(shí)間字符串 * @return * @author kejie.huang * @date 2019/10/10 11:19 */ public static String formatDateToParse(Date currentDate, String formateStr) { LocalDateTime localDateTime = dateConvertLocalDateTime(currentDate); DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(formateStr); return dateTimeFormatter.format(localDateTime); } /** * @description 根據(jù)開始時(shí)間,結(jié)束時(shí)間進(jìn)行對(duì)比 * @param startDate 開始時(shí)間 * @param endDate 結(jié)束時(shí)間 * @return * @author kejie.huang * @date 2019/10/10 11:16 */ public static boolean compareDay(Date startDate, Date endDate) { return startDate.before(endDate); } /** * @title DateUtils * @Description獲取本月第一天 * @author kejie.huang * @Date 2019/10/14 10:52 * @Copyright 2019-2020 */ public static Date getMonthFirstDay() { LocalDateTime date = LocalDateTime.now(); LocalDateTime firstday = date.with(TemporalAdjusters.firstDayOfMonth()); return localDateTimeConvertDate(firstday); } /** * @title DateUtils * @Description獲取本月最后一天 * @author kejie.huang * @Date 2019/10/14 10:52 * @Copyright 2019-2020 */ public static Date getMonthLastDay() { LocalDateTime date = LocalDateTime.now(); LocalDateTime lastday = date.with(TemporalAdjusters.lastDayOfMonth()); return localDateTimeConvertDate(lastday); } /** * @title DateUtils * @Description 根據(jù)localDateTime轉(zhuǎn)換成ZonedDateTime對(duì)象,用于把localDatTime轉(zhuǎn)成Date * @author kejie.huang * @Date 2019/10/14 11:03 * @Copyright 2019-2020 */ public static ZonedDateTime getZonedDateTimeByLocalDateTime(LocalDateTime localDateTime) { ZoneId zoneId = ZoneId.systemDefault(); ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId); return zonedDateTime; } /** * @return * @description 獲得某天最大時(shí)間 2019-10-14 23:59:59 * @author kejie.huang * @date 2019/10/14 10:56 */ public static Date getEndOfDay(Date date) { LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault()); ; LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX); return localDateTimeConvertDate(endOfDay); } /** * @return * @description 獲得某天最小時(shí)間 2019-10-14 00:00:00 * @author kejie.huang * @date 2019/10/14 10:57 */ public static Date getStartOfDay(Date date) { LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault()); LocalDateTime startOfDay = localDateTime.with(LocalTime.MIN); return localDateTimeConvertDate(startOfDay); } }
到此,相信大家對(duì)“JAVA Date時(shí)間工具類的實(shí)例用法”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。