本篇內(nèi)容主要講解“JAVA Date時間工具類的實例用法”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“JAVA Date時間工具類的實例用法”吧!
從策劃到設(shè)計制作,每一步都追求做到細膩,制作可持續(xù)發(fā)展的企業(yè)網(wǎng)站。為客戶提供成都做網(wǎng)站、網(wǎng)站設(shè)計、網(wǎng)站策劃、網(wǎng)頁設(shè)計、域名注冊、網(wǎng)站空間、網(wǎng)絡(luò)營銷、VI設(shè)計、 網(wǎng)站改版、漏洞修補等服務(wù)。為客戶提供更好的一站式互聯(lián)網(wǎng)解決方案,以客戶的口碑塑造優(yōu)易品牌,攜手廣大客戶,共同發(fā)展進步。
import java.text.ParseException; import java.time.*; import java.time.format.DateTimeFormatter; import java.time.temporal.TemporalAdjusters; import java.util.Date; /** * @description: 時間工具類 * @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)建時間 * @return * @throws ParseException */ public static Date plusDay(int num, Date currentDate) { LocalDateTime localDateTime = dateConvertLocalDateTime(currentDate); localDateTime = localDateTime.plusDays(num); return localDateTimeConvertDate(localDateTime); } /** * @description 根據(jù)時間字符串轉(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ù)時間轉(zhuǎn)換成時間字符串 * @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ù)開始時間,結(jié)束時間進行對比 * @param startDate 開始時間 * @param endDate 結(jié)束時間 * @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對象,用于把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 獲得某天最大時間 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 獲得某天最小時間 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); } }
到此,相信大家對“JAVA Date時間工具類的實例用法”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!