1. import java.text.DateFormat;
    2. import java.text.SimpleDateFormat;
    3. import java.time.Instant;
    4. import java.time.LocalDate;
    5. import java.time.LocalDateTime;
    6. import java.time.ZoneId;
    7. import java.time.ZonedDateTime;
    8. import java.util.Calendar;
    9. import java.util.Date;
    10. /**
    11. * 常用日期处理
    12. **/
    13. public class DateUtil {
    14. /**
    15. * date 与 localdate(time)互转
    16. * 方法名以as开头
    17. * ---------------------------------------------------------------------------------
    18. */
    19. public static Date asDate(LocalDate localDate) {
    20. return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
    21. }
    22. public static Date asDate(LocalDateTime localDateTime) {
    23. return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    24. }
    25. public static LocalDate asLocalDate(Date date) {
    26. return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
    27. }
    28. public static LocalDateTime asLocalDateTime(Date date) {
    29. return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
    30. }
    31. /**
    32. * 日期与字符串的互转
    33. * 方法名以trans开头
    34. * ---------------------------------------------------------------------------------
    35. */
    36. /**
    37. * 日期转字符串
    38. *
    39. * @param date 日期
    40. * @return 日期字符串
    41. */
    42. public static String transToLongStr(Date date) {
    43. DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    44. return dateFormat.format(date);
    45. }
    46. /**
    47. * 日期转字符串
    48. *
    49. * @param date 日期
    50. * @return 日期字符串
    51. */
    52. public static String transToShortStr(Date date) {
    53. DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    54. return dateFormat.format(date);
    55. }
    56. /**
    57. * 字符串转日期类型,采用默认格式
    58. *
    59. * @param date 日期字符串
    60. * @return 日期
    61. * @throws Exception
    62. */
    63. public static Date transToLongDate(String date) throws Exception {
    64. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    65. return sdf.parse(date);
    66. }
    67. /**
    68. * 字符串转日期类型,采用默认格式
    69. *
    70. * @param date 日期字符串
    71. * @return 日期
    72. * @throws Exception
    73. */
    74. public static Date transToShortDate(String date) throws Exception {
    75. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    76. return sdf.parse(date);
    77. }
    78. /**
    79. * 字符串砖日期类型, 采用指定格式
    80. *
    81. * @param date 日期字符串
    82. * @param pattern 格式
    83. * @return 日期
    84. * @throws Exception
    85. */
    86. public static Date transToDate(String date, String pattern) throws Exception {
    87. SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    88. return sdf.parse(date);
    89. }
    90. /**
    91. * 字符串砖日期类型, 采用指定格式
    92. *
    93. * @param date 日期字符串
    94. * @param pattern 格式
    95. * @return 日期
    96. * @throws Exception
    97. */
    98. public static String transToStr(Date date, String pattern) throws Exception {
    99. DateFormat dateFormat = new SimpleDateFormat(pattern);
    100. return dateFormat.format(date);
    101. }
    102. /**
    103. * 其它常用
    104. * ---------------------------------------------------------------------------------
    105. */
    106. /**
    107. * 从指定日期开始, n年后,总共有多少天
    108. *
    109. * @param startDate 开始时间
    110. * @param n n年后
    111. * @return 这两个时间之间的天数
    112. */
    113. public static int getDaysAfterYearDate(Date startDate, int n) {
    114. LocalDate localDate = startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    115. LocalDate nUear = localDate.plusYears(n);
    116. long be = nUear.toEpochDay() - localDate.toEpochDay();
    117. return Integer.valueOf(String.valueOf(be));
    118. }
    119. /**
    120. * 得到n年后的时间
    121. *
    122. * @param startDate 开始时间
    123. * @param n n年后
    124. * @return n后年的时间
    125. */
    126. public static Date getDateAfterNyears(Date startDate, int n) {
    127. LocalDateTime localDateTime = startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    128. LocalDateTime nnextYear = localDateTime.plusYears(n);
    129. ZoneId zoneId = ZoneId.systemDefault();
    130. ZonedDateTime zdt = nnextYear.atZone(zoneId);
    131. return Date.from(zdt.toInstant());
    132. }
    133. /**
    134. * 得到n天后的时间
    135. *
    136. * @param startDate 开始时间
    137. * @param n n天后
    138. * @return n天后的时间
    139. */
    140. public static Date getDateAfterNdays(Date startDate, int n) {
    141. LocalDateTime localDateTime = startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    142. LocalDateTime nextDay = localDateTime.plusDays(n);
    143. ZoneId zoneId = ZoneId.systemDefault();
    144. ZonedDateTime zdt = nextDay.atZone(zoneId);
    145. return Date.from(zdt.toInstant());
    146. }
    147. /**
    148. * 得到n月后的时间
    149. *
    150. * @param startDate 开始时间
    151. * @param n n天后
    152. * @return n天后的时间
    153. */
    154. public static Date getDateAfterNmonths(Date startDate, int n) {
    155. LocalDateTime localDateTime = startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    156. LocalDateTime nextDay = localDateTime.plusMonths(n);
    157. ZoneId zoneId = ZoneId.systemDefault();
    158. ZonedDateTime zdt = nextDay.atZone(zoneId);
    159. return Date.from(zdt.toInstant());
    160. }
    161. /**
    162. * 日期转时间戳 (毫秒)
    163. *
    164. * @param date
    165. * @return
    166. */
    167. public static Long dateToLong(Date date) {
    168. if (date != null) {
    169. return date.getTime();
    170. }
    171. return null;
    172. }
    173. /**
    174. * 时间戳(毫秒)转日期
    175. *
    176. * @param d
    177. * @return
    178. */
    179. public static Date longToDate(Long d) {
    180. if (d != null) {
    181. return new Date(d);
    182. }
    183. return null;
    184. }
    185. /**
    186. * 比较两个时间点相差多少年
    187. *
    188. * @param st 开始时间
    189. * @param ed 结束时间
    190. * @return 间隔年数
    191. */
    192. public static int getYearNumByCompareDate(Date st, Date ed) {
    193. Calendar start = Calendar.getInstance();
    194. Calendar end = Calendar.getInstance();
    195. try {
    196. start.setTime(st);
    197. end.setTime(ed);
    198. } catch (Exception e) {
    199. e.printStackTrace();
    200. }
    201. if (end.get(Calendar.YEAR) > start.get(Calendar.YEAR)) {
    202. int year = end.get(Calendar.YEAR) - start.get(Calendar.YEAR);
    203. if (end.get(Calendar.MONTH) + 1 >= start.get(Calendar.MONTH) + 1) {
    204. if (end.get(Calendar.DATE) >= start.get(Calendar.DATE)) {
    205. return year;
    206. } else {
    207. return year - 1;
    208. }
    209. } else {
    210. return year - 1;
    211. }
    212. } else {
    213. return 0;
    214. }
    215. }
    216. }