@Component public class DateUtil { public final static String EMPTY_SRING = ""; public final static String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss"; public final static long DATE_TIME = 1000 * 24 * 60 * 60; public final static long HOUR_TIME = 1000 * 60 * 60; public final static long MINUTE_TIME = 1000 * 60; public LocalDateTime parseDate(String timeStr) { // return new LocalDateTimeStringConverter().fromString(timeStr); DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); //嚴格按照ISO yyyy-MM-dd驗證,03寫成3都不行 return LocalDateTime.parse(timeStr, pattern); } public String currentDateDay() { LocalDate ldt = LocalDate.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); return ldt.format(formatter); } public String currentDateTime() { LocalDateTime ldt = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); return ldt.format(formatter); } public String afterCurrentDate(LocalDateTime dt, int mount, int unit) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); LocalDateTime ldt = LocalDateTime.now(); if (dt != null) { ldt = dt; } if (unit == ConstUtil.UNIT_SECOND) { return ldt.plusSeconds(mount).format(formatter); } else if (unit == ConstUtil.UNIT_MINUTE) { return ldt.plusMinutes(mount).format(formatter); } else if (unit == ConstUtil.UNIT_HOUR) { return ldt.plusHours(mount).format(formatter); } else if (unit == ConstUtil.UNIT_DAY) { return ldt.plusDays(mount).format(formatter); } else if (unit == ConstUtil.UNIT_WEEK) { return ldt.plusWeeks(mount).format(formatter); } else if (unit == ConstUtil.UNIT_MONTH) { return ldt.plusMonths(mount).format(formatter); } else if (unit == ConstUtil.UNIT_YEAR) { return ldt.plusYears(mount).format(formatter); } return ldt.format(formatter); } public String beforeCurrentDate(LocalDateTime dt, int mount, int unit) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); LocalDateTime ldt = LocalDateTime.now(); if (dt != null) { ldt = dt; } if (unit == ConstUtil.UNIT_SECOND) { return ldt.minusSeconds(mount).format(formatter); } else if (unit == ConstUtil.UNIT_MINUTE) { return ldt.minusMinutes(mount).format(formatter); } else if (unit == ConstUtil.UNIT_HOUR) { return ldt.minusHours(mount).format(formatter); } else if (unit == ConstUtil.UNIT_DAY) { return ldt.minusDays(mount).format(formatter); } else if (unit == ConstUtil.UNIT_WEEK) { return ldt.minusWeeks(mount).format(formatter); } else if (unit == ConstUtil.UNIT_MONTH) { return ldt.minusMonths(mount).format(formatter); } else if (unit == ConstUtil.UNIT_YEAR) { return ldt.minusYears(mount).format(formatter); } return ldt.format(formatter); } /** * @param date * @return Date */ public static Date convertStringToDate(String date) { try { return new SimpleDateFormat(DEFAULT_PATTERN).parse(date); } catch (ParseException e) { return null; } } /** * 把日期轉換成yyyy-MM-dd HH:mm:ss格式 * * @param date * @return String */ public static String convertDate(Date date) { if (date == null) { return EMPTY_SRING; } return new SimpleDateFormat(DEFAULT_PATTERN).format(date); } /** * 把時間加上day天后返回,若是傳負數表明減day天 * * @param date * @param day * @return Date */ public static Date dateAdd(Date date, int day) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + day); return calendar.getTime(); } }