import java.text.DateFormat;import java.text.SimpleDateFormat;import java.time.Instant;import java.time.LocalDate;import java.time.LocalDateTime;import java.time.ZoneId;import java.time.ZonedDateTime;import java.util.Calendar;import java.util.Date;/** * 常用日期处理 **/public class DateUtil { /** * date 与 localdate(time)互转 * 方法名以as开头 * --------------------------------------------------------------------------------- */ public static Date asDate(LocalDate localDate) { return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()); } public static Date asDate(LocalDateTime localDateTime) { return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()); } public static LocalDate asLocalDate(Date date) { return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate(); } public static LocalDateTime asLocalDateTime(Date date) { return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime(); } /** * 日期与字符串的互转 * 方法名以trans开头 * --------------------------------------------------------------------------------- */ /** * 日期转字符串 * * @param date 日期 * @return 日期字符串 */ public static String transToLongStr(Date date) { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return dateFormat.format(date); } /** * 日期转字符串 * * @param date 日期 * @return 日期字符串 */ public static String transToShortStr(Date date) { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); return dateFormat.format(date); } /** * 字符串转日期类型,采用默认格式 * * @param date 日期字符串 * @return 日期 * @throws Exception */ public static Date transToLongDate(String date) throws Exception { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.parse(date); } /** * 字符串转日期类型,采用默认格式 * * @param date 日期字符串 * @return 日期 * @throws Exception */ public static Date transToShortDate(String date) throws Exception { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); return sdf.parse(date); } /** * 字符串砖日期类型, 采用指定格式 * * @param date 日期字符串 * @param pattern 格式 * @return 日期 * @throws Exception */ public static Date transToDate(String date, String pattern) throws Exception { SimpleDateFormat sdf = new SimpleDateFormat(pattern); return sdf.parse(date); } /** * 字符串砖日期类型, 采用指定格式 * * @param date 日期字符串 * @param pattern 格式 * @return 日期 * @throws Exception */ public static String transToStr(Date date, String pattern) throws Exception { DateFormat dateFormat = new SimpleDateFormat(pattern); return dateFormat.format(date); } /** * 其它常用 * --------------------------------------------------------------------------------- */ /** * 从指定日期开始, n年后,总共有多少天 * * @param startDate 开始时间 * @param n n年后 * @return 这两个时间之间的天数 */ public static int getDaysAfterYearDate(Date startDate, int n) { LocalDate localDate = startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); LocalDate nUear = localDate.plusYears(n); long be = nUear.toEpochDay() - localDate.toEpochDay(); return Integer.valueOf(String.valueOf(be)); } /** * 得到n年后的时间 * * @param startDate 开始时间 * @param n n年后 * @return n后年的时间 */ public static Date getDateAfterNyears(Date startDate, int n) { LocalDateTime localDateTime = startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); LocalDateTime nnextYear = localDateTime.plusYears(n); ZoneId zoneId = ZoneId.systemDefault(); ZonedDateTime zdt = nnextYear.atZone(zoneId); return Date.from(zdt.toInstant()); } /** * 得到n天后的时间 * * @param startDate 开始时间 * @param n n天后 * @return n天后的时间 */ public static Date getDateAfterNdays(Date startDate, int n) { LocalDateTime localDateTime = startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); LocalDateTime nextDay = localDateTime.plusDays(n); ZoneId zoneId = ZoneId.systemDefault(); ZonedDateTime zdt = nextDay.atZone(zoneId); return Date.from(zdt.toInstant()); } /** * 得到n月后的时间 * * @param startDate 开始时间 * @param n n天后 * @return n天后的时间 */ public static Date getDateAfterNmonths(Date startDate, int n) { LocalDateTime localDateTime = startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); LocalDateTime nextDay = localDateTime.plusMonths(n); ZoneId zoneId = ZoneId.systemDefault(); ZonedDateTime zdt = nextDay.atZone(zoneId); return Date.from(zdt.toInstant()); } /** * 日期转时间戳 (毫秒) * * @param date * @return */ public static Long dateToLong(Date date) { if (date != null) { return date.getTime(); } return null; } /** * 时间戳(毫秒)转日期 * * @param d * @return */ public static Date longToDate(Long d) { if (d != null) { return new Date(d); } return null; } /** * 比较两个时间点相差多少年 * * @param st 开始时间 * @param ed 结束时间 * @return 间隔年数 */ public static int getYearNumByCompareDate(Date st, Date ed) { Calendar start = Calendar.getInstance(); Calendar end = Calendar.getInstance(); try { start.setTime(st); end.setTime(ed); } catch (Exception e) { e.printStackTrace(); } if (end.get(Calendar.YEAR) > start.get(Calendar.YEAR)) { int year = end.get(Calendar.YEAR) - start.get(Calendar.YEAR); if (end.get(Calendar.MONTH) + 1 >= start.get(Calendar.MONTH) + 1) { if (end.get(Calendar.DATE) >= start.get(Calendar.DATE)) { return year; } else { return year - 1; } } else { return year - 1; } } else { return 0; } }}