Java7和Java8日期計算工具類
import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.Period; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; import java.time.temporal.TemporalUnit; import java.util.Calendar; import java.util.Date; public class DateUtils { private static final String DATE_FORMAT = "yyyy-MM-dd"; private static final String TIME_FORMAT = "HH:mm:ss"; private static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss"; private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd"); private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss"); private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); /** * 格式化日期 * * @param date * @return yyyy-MM-dd */ public static String getFormatDate(Date date) { SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT); return format.format(date); } /** * 格式化時間 * * @param date * @return HH:mm:ss */ public static String getFormatTime(Date date) { SimpleDateFormat format = new SimpleDateFormat(TIME_FORMAT); return format.format(date); } /** * 格式化日期及時間 * * @param date * @return yyyy-MM-dd HH:mm:ss */ public static String getFormatDateTime(Date date) { SimpleDateFormat format = new SimpleDateFormat(DATE_TIME_FORMAT); return format.format(date); } /** * 格式化日期 * * @param localDate * @return */ public static String getFormatDate(LocalDate localDate) { return localDate.format(DATE_FORMATTER); } /** * 格式化時間 * * @param localDate * @return */ public static String getFormatTime(LocalDateTime localDateTime) { return localDateTime.format(TIME_FORMATTER); } /** * 格式化日期及時間 * * @param localDate * @return */ public static String getFormatDateTime(LocalDateTime localDateTime) { return localDateTime.format(DATE_TIME_FORMATTER); } /** * 日期時間字符串轉LocalDateTime * * @param dateStr * @return */ public static LocalDateTime dateStrToLocalDateTime(String dateStr) { return LocalDateTime.parse(dateStr, DATE_TIME_FORMATTER); } /** * 日期字符串轉LocalDate * * @param dateStr * @return */ public static LocalDate dateStrToLocalDate(String dateStr) { return LocalDate.parse(dateStr, DATE_FORMATTER); } /** * 日期字符串 轉 Date * * @param dateStr * @return */ public static Date dateStrToDate(String dateStr) { return Date.from(LocalDate.parse(dateStr, DATE_FORMATTER).atStartOfDay(ZoneId.systemDefault()).toInstant()); } /** * 日期時間字符串 轉 Date * * @param dateStr * @return */ public static Date dateTimeStrToDate(String dateStr) { return Date.from(LocalDateTime.parse(dateStr, DATE_TIME_FORMATTER).atZone(ZoneId.systemDefault()).toInstant()); } /** * LocalDate 轉 Date * * @param localDate * @return */ public static Date localDateToDate(LocalDate localDate) { return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()); } /** * LocalDateTime 轉 Date * * @param localDateTime * @return */ public static Date localDateTimeToDate(LocalDateTime localDateTime) { return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()); } /** * Date 轉 LocalDate * * @return */ public static LocalDate dateToLocalDate(Date date) { return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); } /** * Date 轉 LocalDateTime * * @param date * @return */ public static LocalDateTime dateToLocalDateTime(Date date) { return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); } /** * 計算兩個日期時間差 * * @param start yyyy-MM-dd HH:mm:ss * @param end yyyy-MM-dd HH:mm:ss * @return */ public static long dateDifference(String start, String end) { SimpleDateFormat format = new SimpleDateFormat(DATE_TIME_FORMAT); try { Date startTime = format.parse(start); Date endTime = format.parse(end); return endTime.getTime() - startTime.getTime(); } catch (ParseException e) { e.printStackTrace(); } return -1; } /** * 計算兩個日期之間的天數差 * * @param start 2018-03-01 12:00:00 * @param end 2018-03-12 12:00:00 * @return */ public static long calculationDays(String start, String end) { SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { Date fromDate = simpleFormat.parse(start); Date toDate = simpleFormat.parse(end); long from = fromDate.getTime(); long to = toDate.getTime(); long days = (int) ((to - from) / (1000 * 60 * 60 * 24)); return days; } catch (ParseException e) { e.printStackTrace(); } return -1; } /** * 計算小時差 * * @param start * @param end * @return */ public static long calculationHours(String start, String end) { SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { Date fromDate = simpleFormat.parse(start); Date toDate = simpleFormat.parse(end); long from = fromDate.getTime(); long to = toDate.getTime(); long hours = (int) ((to - from) / (1000 * 60 * 60)); return hours; } catch (ParseException e) { e.printStackTrace(); } return -1; } /** * 計算分鍾差 * * @param start * @param end * @return */ public static long calculationMinutes(String start, String end) { SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { Date fromDate = simpleFormat.parse(start); Date toDate = simpleFormat.parse(end); long from = fromDate.getTime(); long to = toDate.getTime(); long minutes = (int) ((to - from) / (1000 * 60)); return minutes; } catch (ParseException e) { e.printStackTrace(); } return -1; } /** * 計算兩個日期之間的秒數差 * @param start * @param end * @return */ public static long calculationSecond(String start, String end) { SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { Date fromDate = simpleFormat.parse(start); Date toDate = simpleFormat.parse(end); long from = fromDate.getTime(); long to = toDate.getTime(); long seconds = (int) ((to - from) / 1000); return seconds; } catch (ParseException e) { e.printStackTrace(); } return -1; } /** * 獲取兩個日期的差 field參數為ChronoUnit.* * @param startTime * @param endTime * @param field 單位(年月日時分秒) * @return */ public static long betweenTwoTime(LocalDateTime startTime, LocalDateTime endTime, ChronoUnit field) { Period period = Period.between(LocalDate.from(startTime), LocalDate.from(endTime)); if (field == ChronoUnit.YEARS) return period.getYears(); if (field == ChronoUnit.MONTHS) return period.getYears() * 12 + period.getMonths(); return field.between(startTime, endTime); } /** * 日期加上一個數,根據field不同加不同值,field為ChronoUnit.* * @param time * @param number * @param field * @return */ public static LocalDateTime plus(LocalDateTime time, long number, TemporalUnit field) { return time.plus(number, field); } /** * 日期減去一個數,根據field不同減不同值,field參數為ChronoUnit.* * @param time * @param number * @param field * @return */ public static LocalDateTime minu(LocalDateTime time, long number, TemporalUnit field){ return time.minus(number,field); } /** * 根據field不同加減不同值 * @param date * @param field Calendar.YEAR * @param number 1000/-1000 */ public static Date calculationDate(Date date, int field, int number) { Calendar calendar = Calendar.getInstance(); calendar.add(field, number); return calendar.getTime(); } /** * 比較兩個日期先后 * @param date1 * @param date2 * @return */ public static boolean compareDate(Date firstDate, Date secondDate) { return firstDate.getTime() < secondDate.getTime(); } /** * 比較第一個日期是否大於第二個日期 * @param firstDate 第一個日期 * @param secondDate 第二個日期 * @return true-大於;false-不大於 */ public boolean localDateIsBefore(LocalDate firstDate, LocalDate secondDate) { return firstDate.isBefore(secondDate); } public static void main(String[] args) { Date date = new Date(); LocalDateTime localDate = DateUtils.dateToLocalDateTime(date); System.out.println(localDate); String start = "2020-03-01 12:00:00"; String end = "2022-03-12 12:01:00"; long seconds = DateUtils.calculationSecond(start, end); System.out.println("相差秒數: " + seconds); long minutes = DateUtils.calculationMinutes(start, end); System.out.println("相差分鍾數: " + minutes); long days = DateUtils.calculationDays(start, end); System.out.println("相差天數: " + days); LocalDateTime startTime = LocalDateTime.of(2000, 10, 13, 11, 11); LocalDateTime endTime = LocalDateTime.of(2000, 10, 14, 13, 13); long time = DateUtils.betweenTwoTime(startTime, endTime, ChronoUnit.SECONDS); System.out.println(time); date = DateUtils.calculationDate(date, Calendar.DATE, -10); System.out.println("減去10天: " + DateUtils.getFormatDate(date)); } }