您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)Java8中的日期時(shí)間工具類有哪些,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。
具體如下
/** * Java8日期時(shí)間工具類 * * @author JourWon * @date 2020/12/13 */ public class LocalDateUtils { /** * 顯示年月日時(shí)分秒,例如 2015-08-11 09:51:53. */ public static final String DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss"; /** * 僅顯示年月日,例如 2015-08-11. */ public static final String DATE_PATTERN = "yyyy-MM-dd"; /** * 僅顯示時(shí)分秒,例如 09:51:53. */ public static final String TIME_PATTERN = "HH:mm:ss"; /** * 顯示年月日時(shí)分秒(無(wú)符號(hào)),例如 20150811095153. */ public static final String UNSIGNED_DATETIME_PATTERN = "yyyyMMddHHmmss"; /** * 僅顯示年月日(無(wú)符號(hào)),例如 20150811. */ public static final String UNSIGNED_DATE_PATTERN = "yyyyMMdd"; /** * 春天; */ public static final Integer SPRING = 1; /** * 夏天; */ public static final Integer SUMMER = 2; /** * 秋天; */ public static final Integer AUTUMN = 3; /** * 冬天; */ public static final Integer WINTER = 4; /** * 星期日; */ public static final String SUNDAY = "星期日"; /** * 星期一; */ public static final String MONDAY = "星期一"; /** * 星期二; */ public static final String TUESDAY = "星期二"; /** * 星期三; */ public static final String WEDNESDAY = "星期三"; /** * 星期四; */ public static final String THURSDAY = "星期四"; /** * 星期五; */ public static final String FRIDAY = "星期五"; /** * 星期六; */ public static final String SATURDAY = "星期六"; /** * 年 */ private static final String YEAR = "year"; /** * 月 */ private static final String MONTH = "month"; /** * 周 */ private static final String WEEK = "week"; /** * 日 */ private static final String DAY = "day"; /** * 時(shí) */ private static final String HOUR = "hour"; /** * 分 */ private static final String MINUTE = "minute"; /** * 秒 */ private static final String SECOND = "second"; /** * 獲取當(dāng)前日期和時(shí)間字符串. * * @return String 日期時(shí)間字符串,例如 2015-08-11 09:51:53 */ public static String getLocalDateTimeStr() { return format(LocalDateTime.now(), DATETIME_PATTERN); } /** * 獲取當(dāng)前日期字符串. * * @return String 日期字符串,例如2015-08-11 */ public static String getLocalDateStr() { return format(LocalDate.now(), DATE_PATTERN); } /** * 獲取當(dāng)前時(shí)間字符串. * * @return String 時(shí)間字符串,例如 09:51:53 */ public static String getLocalTimeStr() { return format(LocalTime.now(), TIME_PATTERN); } /** * 獲取當(dāng)前星期字符串. * * @return String 當(dāng)前星期字符串,例如 星期二 */ public static String getDayOfWeekStr() { return format(LocalDate.now(), "E"); } /** * 獲取指定日期是星期幾 * * @param localDate 日期 * @return String 星期幾 */ public static String getDayOfWeekStr(LocalDate localDate) { String[] weekOfDays = {MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY}; int dayOfWeek = localDate.getDayOfWeek().getValue() - 1; return weekOfDays[dayOfWeek]; } /** * 獲取日期時(shí)間字符串 * * @param temporal 需要轉(zhuǎn)化的日期時(shí)間 * @param pattern 時(shí)間格式 * @return String 日期時(shí)間字符串,例如 2015-08-11 09:51:53 */ public static String format(TemporalAccessor temporal, String pattern) { DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern); return dateTimeFormatter.format(temporal); } /** * 日期時(shí)間字符串轉(zhuǎn)換為日期時(shí)間(java.time.LocalDateTime) * * @param localDateTimeStr 日期時(shí)間字符串 * @param pattern 日期時(shí)間格式 例如DATETIME_PATTERN * @return LocalDateTime 日期時(shí)間 */ public static LocalDateTime parseLocalDateTime(String localDateTimeStr, String pattern) { DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern); return LocalDateTime.parse(localDateTimeStr, dateTimeFormatter); } /** * 日期字符串轉(zhuǎn)換為日期(java.time.LocalDate) * * @param localDateStr 日期字符串 * @param pattern 日期格式 例如DATE_PATTERN * @return LocalDate 日期 */ public static LocalDate parseLocalDate(String localDateStr, String pattern) { DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern); return LocalDate.parse(localDateStr, dateTimeFormatter); } /** * 獲取指定日期時(shí)間加上指定數(shù)量日期時(shí)間單位之后的日期時(shí)間. * * @param localDateTime 日期時(shí)間 * @param num 數(shù)量 * @param chronoUnit 日期時(shí)間單位 * @return LocalDateTime 新的日期時(shí)間 */ public static LocalDateTime plus(LocalDateTime localDateTime, int num, ChronoUnit chronoUnit) { return localDateTime.plus(num, chronoUnit); } /** * 獲取指定日期時(shí)間減去指定數(shù)量日期時(shí)間單位之后的日期時(shí)間. * * @param localDateTime 日期時(shí)間 * @param num 數(shù)量 * @param chronoUnit 日期時(shí)間單位 * @return LocalDateTime 新的日期時(shí)間 */ public static LocalDateTime minus(LocalDateTime localDateTime, int num, ChronoUnit chronoUnit) { return localDateTime.minus(num, chronoUnit); } /** * 根據(jù)ChronoUnit計(jì)算兩個(gè)日期時(shí)間之間相隔日期時(shí)間 * * @param start 開(kāi)始日期時(shí)間 * @param end 結(jié)束日期時(shí)間 * @param chronoUnit 日期時(shí)間單位 * @return long 相隔日期時(shí)間 */ public static long getChronoUnitBetween(LocalDateTime start, LocalDateTime end, ChronoUnit chronoUnit) { return Math.abs(start.until(end, chronoUnit)); } /** * 根據(jù)ChronoUnit計(jì)算兩個(gè)日期之間相隔年數(shù)或月數(shù)或天數(shù) * * @param start 開(kāi)始日期 * @param end 結(jié)束日期 * @param chronoUnit 日期時(shí)間單位,(ChronoUnit.YEARS,ChronoUnit.MONTHS,ChronoUnit.WEEKS,ChronoUnit.DAYS) * @return long 相隔年數(shù)或月數(shù)或天數(shù) */ public static long getChronoUnitBetween(LocalDate start, LocalDate end, ChronoUnit chronoUnit) { return Math.abs(start.until(end, chronoUnit)); } /** * 獲取本年第一天的日期字符串 * * @return String 格式:yyyy-MM-dd 00:00:00 */ public static String getFirstDayOfYearStr() { return getFirstDayOfYearStr(LocalDateTime.now()); } /** * 獲取本年最后一天的日期字符串 * * @return String 格式:yyyy-MM-dd 23:59:59 */ public static String getLastDayOfYearStr() { return getLastDayOfYearStr(LocalDateTime.now()); } /** * 獲取指定日期當(dāng)年第一天的日期字符串 * * @param localDateTime 指定日期時(shí)間 * @return String 格式:yyyy-MM-dd 00:00:00 */ public static String getFirstDayOfYearStr(LocalDateTime localDateTime) { return getFirstDayOfYearStr(localDateTime, DATETIME_PATTERN); } /** * 獲取指定日期當(dāng)年最后一天的日期字符串 * * @param localDateTime 指定日期時(shí)間 * @return String 格式:yyyy-MM-dd 23:59:59 */ public static String getLastDayOfYearStr(LocalDateTime localDateTime) { return getLastDayOfYearStr(localDateTime, DATETIME_PATTERN); } /** * 獲取指定日期當(dāng)年第一天的日期字符串,帶日期格式化參數(shù) * * @param localDateTime 指定日期時(shí)間 * @param pattern 日期時(shí)間格式 * @return String 格式:yyyy-MM-dd 00:00:00 */ public static String getFirstDayOfYearStr(LocalDateTime localDateTime, String pattern) { return format(localDateTime.withDayOfYear(1).withHour(0).withMinute(0).withSecond(0), pattern); } /** * 獲取指定日期當(dāng)年最后一天的日期字符串,帶日期格式化參數(shù) * * @param localDateTime 指定日期時(shí)間 * @param pattern 日期時(shí)間格式 * @return String 格式:yyyy-MM-dd 23:59:59 */ public static String getLastDayOfYearStr(LocalDateTime localDateTime, String pattern) { return format(localDateTime.with(TemporalAdjusters.lastDayOfYear()).withHour(23).withMinute(59).withSecond(59), pattern); } /** * 獲取本月第一天的日期字符串 * * @return String 格式:yyyy-MM-dd 00:00:00 */ public static String getFirstDayOfMonthStr() { return getFirstDayOfMonthStr(LocalDateTime.now()); } /** * 獲取本月最后一天的日期字符串 * * @return String 格式:yyyy-MM-dd 23:59:59 */ public static String getLastDayOfMonthStr() { return getLastDayOfMonthStr(LocalDateTime.now()); } /** * 獲取指定日期當(dāng)月第一天的日期字符串 * * @param localDateTime 指定日期時(shí)間 * @return String 格式:yyyy-MM-dd 23:59:59 */ public static String getFirstDayOfMonthStr(LocalDateTime localDateTime) { return getFirstDayOfMonthStr(localDateTime, DATETIME_PATTERN); } /** * 獲取指定日期當(dāng)月最后一天的日期字符串 * * @param localDateTime 指定日期時(shí)間 * @return String 格式:yyyy-MM-dd 23:59:59 */ public static String getLastDayOfMonthStr(LocalDateTime localDateTime) { return getLastDayOfMonthStr(localDateTime, DATETIME_PATTERN); } /** * 獲取指定日期當(dāng)月第一天的日期字符串,帶日期格式化參數(shù) * * @param localDateTime 指定日期時(shí)間 * @return String 格式:yyyy-MM-dd 00:00:00 */ public static String getFirstDayOfMonthStr(LocalDateTime localDateTime, String pattern) { return format(localDateTime.withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0), pattern); } /** * 獲取指定日期當(dāng)月最后一天的日期字符串,帶日期格式化參數(shù) * * @param localDateTime 指定日期時(shí)間 * @param pattern 日期時(shí)間格式 * @return String 格式:yyyy-MM-dd 23:59:59 */ public static String getLastDayOfMonthStr(LocalDateTime localDateTime, String pattern) { return format(localDateTime.with(TemporalAdjusters.lastDayOfMonth()).withHour(23).withMinute(59).withSecond(59), pattern); } /** * 獲取本周第一天的日期字符串 * * @return String 格式:yyyy-MM-dd 00:00:00 */ public static String getFirstDayOfWeekStr() { return getFirstDayOfWeekStr(LocalDateTime.now()); } /** * 獲取本周最后一天的日期字符串 * * @return String 格式:yyyy-MM-dd 23:59:59 */ public static String getLastDayOfWeekStr() { return getLastDayOfWeekStr(LocalDateTime.now()); } /** * 獲取指定日期當(dāng)周第一天的日期字符串,這里第一天為周一 * * @param localDateTime 指定日期時(shí)間 * @return String 格式:yyyy-MM-dd 00:00:00 */ public static String getFirstDayOfWeekStr(LocalDateTime localDateTime) { return getFirstDayOfWeekStr(localDateTime, DATETIME_PATTERN); } /** * 獲取指定日期當(dāng)周最后一天的日期字符串,這里最后一天為周日 * * @param localDateTime 指定日期時(shí)間 * @return String 格式:yyyy-MM-dd 23:59:59 */ public static String getLastDayOfWeekStr(LocalDateTime localDateTime) { return getLastDayOfWeekStr(localDateTime, DATETIME_PATTERN); } /** * 獲取指定日期當(dāng)周第一天的日期字符串,這里第一天為周一,帶日期格式化參數(shù) * * @param localDateTime 指定日期時(shí)間 * @param pattern 日期時(shí)間格式 * @return String 格式:yyyy-MM-dd 00:00:00 */ public static String getFirstDayOfWeekStr(LocalDateTime localDateTime, String pattern) { return format(localDateTime.with(DayOfWeek.MONDAY).withHour(0).withMinute(0).withSecond(0), pattern); } /** * 獲取指定日期當(dāng)周最后一天的日期字符串,這里最后一天為周日,帶日期格式化參數(shù) * * @param localDateTime 指定日期時(shí)間 * @param pattern 日期時(shí)間格式 * @return String 格式:yyyy-MM-dd 23:59:59 */ public static String getLastDayOfWeekStr(LocalDateTime localDateTime, String pattern) { return format(localDateTime.with(DayOfWeek.SUNDAY).withHour(23).withMinute(59).withSecond(59), pattern); } /** * 獲取今天開(kāi)始時(shí)間的日期字符串 * * @return String 格式:yyyy-MM-dd 00:00:00 */ public static String getStartTimeOfDayStr() { return getStartTimeOfDayStr(LocalDateTime.now()); } /** * 獲取今天結(jié)束時(shí)間的日期字符串 * * @return String 格式:yyyy-MM-dd 23:59:59 */ public static String getEndTimeOfDayStr() { return getEndTimeOfDayStr(LocalDateTime.now()); } /** * 獲取指定日期開(kāi)始時(shí)間的日期字符串 * * @param localDateTime 指定日期時(shí)間 * @return String 格式:yyyy-MM-dd 00:00:00 */ public static String getStartTimeOfDayStr(LocalDateTime localDateTime) { return getStartTimeOfDayStr(localDateTime, DATETIME_PATTERN); } /** * 獲取指定日期結(jié)束時(shí)間的日期字符串 * * @param localDateTime 指定日期時(shí)間 * @return String 格式:yyyy-MM-dd 23:59:59 */ public static String getEndTimeOfDayStr(LocalDateTime localDateTime) { return getEndTimeOfDayStr(localDateTime, DATETIME_PATTERN); } /** * 獲取指定日期開(kāi)始時(shí)間的日期字符串,帶日期格式化參數(shù) * * @param localDateTime 指定日期時(shí)間 * @param pattern 日期時(shí)間格式 * @return String 格式:yyyy-MM-dd HH:mm:ss */ public static String getStartTimeOfDayStr(LocalDateTime localDateTime, String pattern) { return format(localDateTime.withHour(0).withMinute(0).withSecond(0), pattern); } /** * 獲取指定日期結(jié)束時(shí)間的日期字符串,帶日期格式化參數(shù) * * @param localDateTime 指定日期時(shí)間 * @param pattern 日期時(shí)間格式 * @return String 格式:yyyy-MM-dd 23:59:59 */ public static String getEndTimeOfDayStr(LocalDateTime localDateTime, String pattern) { return format(localDateTime.withHour(23).withMinute(59).withSecond(59), pattern); } /** * 切割日期。按照周期切割成小段日期段。例如: <br> * * @param startDate 開(kāi)始日期(yyyy-MM-dd) * @param endDate 結(jié)束日期(yyyy-MM-dd) * @param period 周期(天,周,月,年) * @return 切割之后的日期集合 * <li>startDate="2019-02-28",endDate="2019-03-05",period="day"</li> * <li>結(jié)果為:[2019-02-28, 2019-03-01, 2019-03-02, 2019-03-03, 2019-03-04, 2019-03-05]</li><br> * <li>startDate="2019-02-28",endDate="2019-03-25",period="week"</li> * <li>結(jié)果為:[2019-02-28,2019-03-06, 2019-03-07,2019-03-13, 2019-03-14,2019-03-20, * 2019-03-21,2019-03-25]</li><br> * <li>startDate="2019-02-28",endDate="2019-05-25",period="month"</li> * <li>結(jié)果為:[2019-02-28,2019-02-28, 2019-03-01,2019-03-31, 2019-04-01,2019-04-30, * 2019-05-01,2019-05-25]</li><br> * <li>startDate="2019-02-28",endDate="2020-05-25",period="year"</li> * <li>結(jié)果為:[2019-02-28,2019-12-31, 2020-01-01,2020-05-25]</li><br> */ public static List<String> listDateStrs(String startDate, String endDate, String period) { List<String> result = new ArrayList<>(); DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DATE_PATTERN); LocalDate end = LocalDate.parse(endDate, dateTimeFormatter); LocalDate start = LocalDate.parse(startDate, dateTimeFormatter); LocalDate tmp = start; switch (period) { case DAY: while (start.isBefore(end) || start.isEqual(end)) { result.add(start.toString()); start = start.plusDays(1); } break; case WEEK: while (tmp.isBefore(end) || tmp.isEqual(end)) { if (tmp.plusDays(6).isAfter(end)) { result.add(tmp.toString() + "," + end); } else { result.add(tmp.toString() + "," + tmp.plusDays(6)); } tmp = tmp.plusDays(7); } break; case MONTH: while (tmp.isBefore(end) || tmp.isEqual(end)) { LocalDate lastDayOfMonth = tmp.with(TemporalAdjusters.lastDayOfMonth()); if (lastDayOfMonth.isAfter(end)) { result.add(tmp.toString() + "," + end); } else { result.add(tmp.toString() + "," + lastDayOfMonth); } tmp = lastDayOfMonth.plusDays(1); } break; case YEAR: while (tmp.isBefore(end) || tmp.isEqual(end)) { LocalDate lastDayOfYear = tmp.with(TemporalAdjusters.lastDayOfYear()); if (lastDayOfYear.isAfter(end)) { result.add(tmp.toString() + "," + end); } else { result.add(tmp.toString() + "," + lastDayOfYear); } tmp = lastDayOfYear.plusDays(1); } break; default: break; } return result; } public static void main(String[] args) { System.out.println(getLocalDateTimeStr()); System.out.println(getLocalDateStr()); System.out.println(getLocalTimeStr()); System.out.println(getDayOfWeekStr()); System.out.println(getDayOfWeekStr(LocalDate.now())); System.out.println("========"); System.out.println(format(LocalDate.now(), UNSIGNED_DATE_PATTERN)); System.out.println("========"); System.out.println(parseLocalDateTime("2020-12-13 11:14:12", DATETIME_PATTERN)); System.out.println(parseLocalDate("2020-12-13", DATE_PATTERN)); System.out.println("========"); System.out.println(plus(LocalDateTime.now(), 3, ChronoUnit.HOURS)); System.out.println(minus(LocalDateTime.now(), 4, ChronoUnit.DAYS)); System.out.println("========"); System.out.println(getChronoUnitBetween(LocalDateTime.now(), parseLocalDateTime("2020-12-12 12:03:12", DATETIME_PATTERN), ChronoUnit.MINUTES)); System.out.println(getChronoUnitBetween(LocalDate.now(), parseLocalDate("2021-12-12", DATE_PATTERN), ChronoUnit.WEEKS)); System.out.println("========"); System.out.println(getFirstDayOfYearStr()); System.out.println(getFirstDayOfYearStr(parseLocalDateTime("2021-12-12 12:03:12", DATETIME_PATTERN))); System.out.println(getFirstDayOfYearStr(parseLocalDateTime("2021-12-12 12:03:12", DATETIME_PATTERN), UNSIGNED_DATETIME_PATTERN)); System.out.println(getLastDayOfYearStr()); System.out.println(getLastDayOfYearStr(parseLocalDateTime("2021-12-12 12:03:12", DATETIME_PATTERN))); System.out.println(getLastDayOfYearStr(parseLocalDateTime("2021-12-12 12:03:12", DATETIME_PATTERN), UNSIGNED_DATETIME_PATTERN)); System.out.println("========"); System.out.println(getFirstDayOfMonthStr()); System.out.println(getFirstDayOfMonthStr(parseLocalDateTime("2021-12-12 12:03:12", DATETIME_PATTERN))); System.out.println(getFirstDayOfMonthStr(parseLocalDateTime("2021-12-12 12:03:12", DATETIME_PATTERN), UNSIGNED_DATETIME_PATTERN)); System.out.println(getLastDayOfMonthStr()); System.out.println(getLastDayOfMonthStr(parseLocalDateTime("2021-12-12 12:03:12", DATETIME_PATTERN))); System.out.println(getLastDayOfMonthStr(parseLocalDateTime("2021-12-12 12:03:12", DATETIME_PATTERN), UNSIGNED_DATETIME_PATTERN)); System.out.println("========"); System.out.println(getFirstDayOfWeekStr()); System.out.println(getFirstDayOfWeekStr(parseLocalDateTime("2021-12-12 12:03:12", DATETIME_PATTERN))); System.out.println(getFirstDayOfWeekStr(parseLocalDateTime("2021-12-12 12:03:12", DATETIME_PATTERN), UNSIGNED_DATETIME_PATTERN)); System.out.println(getLastDayOfWeekStr()); System.out.println(getLastDayOfWeekStr(parseLocalDateTime("2021-12-12 12:03:12", DATETIME_PATTERN))); System.out.println(getLastDayOfWeekStr(parseLocalDateTime("2021-12-12 12:03:12", DATETIME_PATTERN), UNSIGNED_DATETIME_PATTERN)); System.out.println("========"); System.out.println(getStartTimeOfDayStr()); System.out.println(getStartTimeOfDayStr(parseLocalDateTime("2021-12-12 12:03:12", DATETIME_PATTERN))); System.out.println(getStartTimeOfDayStr(parseLocalDateTime("2021-12-12 12:03:12", DATETIME_PATTERN), UNSIGNED_DATETIME_PATTERN)); System.out.println(getEndTimeOfDayStr()); System.out.println(getEndTimeOfDayStr(parseLocalDateTime("2021-12-12 12:03:12", DATETIME_PATTERN))); System.out.println(getEndTimeOfDayStr(parseLocalDateTime("2021-12-12 12:03:12", DATETIME_PATTERN), UNSIGNED_DATETIME_PATTERN)); System.out.println("========"); List<String> dateStrs = listDateStrs("2019-01-30", "2020-12-13", YEAR); for (String dateStr : dateStrs) { System.out.println(dateStr); } System.out.println("========"); List<String> dateStrs1 = listDateStrs("2019-01-30", "2020-12-13", MONTH); for (String dateStr : dateStrs1) { System.out.println(dateStr); } System.out.println("========"); List<String> dateStrs2 = listDateStrs("2020-12-01", "2020-12-13", DAY); for (String dateStr : dateStrs2) { System.out.println(dateStr); } } }
以上就是Java8中的日期時(shí)間工具類有哪些,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(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)容。