1 public class DateUtil { 2 3 private static final ZoneId ZONE_ID = ZoneId.systemDefault(); 4 private static final DateTimeFormatter DATETIME = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); 5 private static final DateTimeFormatter DATE = DateTimeFormatter.ofPattern("yyyy-MM-dd"); 6 private static final DateTimeFormatter SHORT_DATE = DateTimeFormatter.ofPattern("yyyyMMdd"); 7 8 public static String toDatetimeString(Date date){ 9 return date != null ? DATETIME.format(date.toInstant().atZone(ZONE_ID).toLocalDateTime()) : null; 10 } 11 12 public static String toDateString(Date date){ 13 return date != null ? DATE.format(date.toInstant().atZone(ZONE_ID).toLocalDate()) : null; 14 } 15 16 public static String toShortDateString(Date date){ 17 return date != null ? SHORT_DATE.format(date.toInstant().atZone(ZONE_ID).toLocalDate()) : null; 18 } 19 20 public static Date parseDatetime(String string) { 21 return string != null && !string.isEmpty() ? Date.from(LocalDateTime.parse(string, DATETIME).atZone(ZONE_ID) 22 .toInstant()) : null; 23 } 24 25 public static Date parseDate(String string) { 26 return string != null && !string.isEmpty() ? Date.from(LocalDate.parse(string, DATE).atStartOfDay() 27 .atZone(ZONE_ID).toInstant()) : null; 28 } 29 30 public static String dateAdd(int date) { 31 return DATETIME.format(LocalDate.now().plusDays(date).atStartOfDay()); 32 } 33 34 public static String datetimeAdd(int date) { 35 return DATETIME.format(LocalDateTime.now().plusDays(date)); 36 } 37 38 public static String datetimeAdd(String string, int date) { 39 return string != null && !string.isEmpty() ? DATETIME.format(LocalDateTime.parse(string, DATETIME).plusDays(date)) 40 : null; 41 } 42 }