优点:
1.方便。
Date 只能是日期加时间的格式,而 LocalDate 、LocalTime、LocalDateTime
分别代表日期,时间,日期+时间,非常灵活。再就是后者在日期计算及格式化方面非常简单易用,而Date要繁琐很多。
2.线程安全。
传统时间类不支持多线程安全。
缺点<目前发现的坑>:
1.在比较日期相隔天数时,不要使用Period.between()方法,这个只是当月相隔天数。其实就是:a月b日 - c月d日 = (b-d)日
LocalDateTime:
他的toString()方法,不同其他类,中间有个T。可使用@JsonFormat注解,格式化需要的格式后转成json。前端展示就会显示对应的格式,当然,debug调试的时候,还是会显示T的。
1@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") 2 3@Override 4 public String toString() { 5 return date.toString() + 'T' + time.toString(); 6 }

注意:
比较时,会需要传接口Temporal作为参数,可用它的实现类,比如localDateTime即可。
如果比较相隔的时、分、毫秒时,需要将格式转成精确到小时以后<相隔的时、分、秒都可以只格式化到小时yyyy-MM-dd HH即可不报错>
附工具类代码:
1public class DateUtil { 2 3 public static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HHmmss"); 4 public static final DateTimeFormatter MONTH_FORMATTER = DateTimeFormatter.ofPattern("yyyyMM"); 5 public static final DateTimeFormatter SHORT_DATE_FORMATTER = DateTimeFormatter.ofPattern("yyMMdd"); 6 public static final DateTimeFormatter SHORT_DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyMMddHHmmss"); 7 public static final DateTimeFormatter DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); 8 public static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd"); 9 10 /** 11 * 返回当前的日期 12 * @return 13 */ 14 public static LocalDate getCurrentLocalDate() { 15 return LocalDate.now(); 16 } 17 18 /** 19 * 返回当前时间 20 * @return 21 */ 22 public static LocalTime getCurrentLocalTime() { 23 return LocalTime.now(); 24 } 25 26 /** 27 * 返回当前日期时间 28 * @return 29 */ 30 public static LocalDateTime getCurrentLocalDateTime() { 31 return LocalDateTime.now(); 32 } 33 34 /** 35 * yyyyMMdd 36 * 37 * @return 38 */ 39 public static String getCurrentDateStr() { 40 return LocalDate.now().format(DATE_FORMATTER); 41 } 42 43 /** 44 * yyMMdd 45 * 46 * @return 47 */ 48 public static String getCurrentShortDateStr() { 49 return LocalDate.now().format(SHORT_DATE_FORMATTER); 50 } 51 52 public static String getCurrentMonthStr() { 53 return LocalDate.now().format(MONTH_FORMATTER); 54 } 55 56 /** 57 * yyyyMMddHHmmss 58 * @return 59 */ 60 public static String getCurrentDateTimeStr() { 61 return LocalDateTime.now().format(DATETIME_FORMATTER); 62 } 63 64 /** 65 * yyMMddHHmmss 66 * @return 67 */ 68 public static String getCurrentShortDateTimeStr() { 69 return LocalDateTime.now().format(SHORT_DATETIME_FORMATTER); 70 } 71 72 /** 73 * HHmmss 74 * @return 75 */ 76 public static String getCurrentTimeStr() { 77 return LocalTime.now().format(TIME_FORMATTER); 78 } 79 80 public static String getCurrentDateStr(String pattern) { 81 return LocalDate.now().format(DateTimeFormatter.ofPattern(pattern)); 82 } 83 84 public static String getCurrentDateTimeStr(String pattern) { 85 return LocalDateTime.now().format(DateTimeFormatter.ofPattern(pattern)); 86 } 87 88 public static String getCurrentTimeStr(String pattern) { 89 return LocalTime.now().format(DateTimeFormatter.ofPattern(pattern)); 90 } 91 92 public static LocalDate parseLocalDate(String dateStr, String pattern) { 93 return LocalDate.parse(dateStr, DateTimeFormatter.ofPattern(pattern)); 94 } 95 96 public static LocalDateTime parseLocalDateTime(String dateTimeStr, String pattern) { 97 return LocalDateTime.parse(dateTimeStr, DateTimeFormatter.ofPattern(pattern)); 98 } 99 100 public static LocalTime parseLocalTime(String timeStr, String pattern) { 101 return LocalTime.parse(timeStr, DateTimeFormatter.ofPattern(pattern)); 102 } 103 104 public static String formatLocalDate(LocalDate date, String pattern) { 105 return date.format(DateTimeFormatter.ofPattern(pattern)); 106 } 107 108 public static String formatLocalDateTime(LocalDateTime datetime, String pattern) { 109 return datetime.format(DateTimeFormatter.ofPattern(pattern)); 110 } 111 112 public static String formatLocalTime(LocalTime time, String pattern) { 113 return time.format(DateTimeFormatter.ofPattern(pattern)); 114 } 115 116 public static LocalDate parseLocalDate(String dateStr) { 117 return LocalDate.parse(dateStr, DATE_FORMATTER); 118 } 119 120 public static LocalDateTime parseLocalDateTime(String dateTimeStr) { 121 return LocalDateTime.parse(dateTimeStr, DATETIME_FORMATTER); 122 } 123 124 public static LocalTime parseLocalTime(String timeStr) { 125 return LocalTime.parse(timeStr, TIME_FORMATTER); 126 } 127 128 public static String formatLocalDate(LocalDate date) { 129 return date.format(DATE_FORMATTER); 130 } 131 132 public static String formatLocalDateTime(LocalDateTime datetime) { 133 return datetime.format(DATETIME_FORMATTER); 134 } 135 136 public static String formatLocalTime(LocalTime time) { 137 return time.format(TIME_FORMATTER); 138 } 139 140 /** 141 * 日期相隔天数 142 * @param startDateInclusive 143 * @param endDateExclusive 144 * @return 145 */ 146 public static long periodDays(LocalDate startDateInclusive, LocalDate endDateExclusive) { 147 return endDateExclusive.toEpochDay() - startDateInclusive.toEpochDay(); 148 } 149 150 /** 151 * 日期相隔小时 152 * @param startInclusive 153 * @param endExclusive 154 * @return 155 */ 156 public static long durationHours(Temporal startInclusive, Temporal endExclusive) { 157 return Duration.between(startInclusive, endExclusive).toHours(); 158 } 159 160 /** 161 * 日期相隔分钟 162 * @param startInclusive 163 * @param endExclusive 164 * @return 165 */ 166 public static long durationMinutes(Temporal startInclusive, Temporal endExclusive) { 167 return Duration.between(startInclusive, endExclusive).toMinutes(); 168 } 169 170 /** 171 * 日期相隔毫秒数 172 * @param startInclusive 173 * @param endExclusive 174 * @return 175 */ 176 public static long durationMillis(Temporal startInclusive, Temporal endExclusive) { 177 return Duration.between(startInclusive, endExclusive).toMillis(); 178 } 179 180 /** 181 * 是否当天 182 * @param date 183 * @return 184 */ 185 public static boolean isToday(LocalDate date) { 186 return getCurrentLocalDate().equals(date); 187 } 188 189 /** 190 * 获取此日期时间与默认时区<Asia/Shanghai>组合的时间毫秒数 191 * @param dateTime 192 * @return 193 */ 194 public static Long toEpochMilli(LocalDateTime dateTime) { 195 return dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); 196 } 197 198 /** 199 * 获取此日期时间与指定时区组合的时间毫秒数 200 * @param dateTime 201 * @return 202 */ 203 public static Long toSelectEpochMilli(LocalDateTime dateTime, ZoneId zoneId) { 204 return dateTime.atZone(zoneId).toInstant().toEpochMilli(); 205 } 206}