1.日期轉換工具類示例代碼以下(通常都是date2比date1大):工具
1 ** 2 *<p> Description: 日期工具類 </p> 3 *<p> Copyright: Copyright(c) 2018/12/26 </p> 4 *<p> Company: xxx</p> 5 * 6 *@author Jason 7 *@Version 1.0 2018/12/26 11:06 8 */ 9 public class MyDateUtil { 10 11 /** 12 * 將指定的日期字符串轉換成日期 13 * @param dateStr 日期字符串 14 * @param pattern 格式 15 * @return 日期對象 16 */ 17 public static Date parseDate(String dateStr, String pattern) 18 { 19 SimpleDateFormat sdf = new SimpleDateFormat(pattern); 20 Date date; 21 try { 22 date = sdf.parse(dateStr); 23 } catch (ParseException e) { 24 throw new RuntimeException("日期轉化錯誤"); 25 } 26 27 return date; 28 } 29 30 /** 31 * 將指定的日期格式化成指定的日期字符串 32 * @param date 日期對象 33 * @param pattern 格式 34 * @return 格式化後的日期字符串 35 */ 36 public static String dateFormate(Date date, String pattern) 37 { 38 SimpleDateFormat sdf = new SimpleDateFormat(pattern); 39 String dateStr; 40 if(date == null) 41 { 42 return ""; 43 } 44 dateStr = sdf.format(date); 45 return dateStr; 46 } 47 48 /** 49 * 查詢指定日期先後指定的天數 50 * @param date 日期對象 51 * @param days 天數 52 * @return 日期對象 53 */ 54 public static Date incr(Date date, int days) 55 { 56 if (date == null){ 57 return null; 58 } 59 60 Calendar calendar = Calendar.getInstance(); 61 calendar.setTime(date); 62 calendar.add(Calendar.DAY_OF_MONTH, days); 63 return calendar.getTime(); 64 } 65 66 /** 67 * 將LocalDate日期轉化成Date 68 * @param localDate LocalDate對象 69 * @return Date對象 70 */ 71 public static Date localDateToDate(LocalDate localDate) 72 { 73 if (localDate == null) 74 { 75 return null; 76 } 77 ZoneId zoneId = ZoneId.systemDefault(); 78 ZonedDateTime zonedDateTime = localDate.atStartOfDay(zoneId); 79 Date date = Date.from(zonedDateTime.toInstant()); 80 81 return date; 82 } 83 84 /** 85 * 將Date轉成LocalDate對象 86 * @param date Date對象 87 * @return LocalDate對象 88 */ 89 public static LocalDate dateToLocalDate(Date date) 90 { 91 if (date == null) 92 { 93 return null; 94 } 95 ZoneId zoneId = ZoneId.systemDefault(); 96 Instant instant = date.toInstant(); 97 LocalDate localDate = instant.atZone(zoneId).toLocalDate(); 98 99 return localDate; 100 } 101 }
2.日期計算示例代碼以下:測試
1 /** 2 *<p> Description: 日期測試類 </p> 3 *<p> Copyright: Copyright(c) 2018/12/27 </p> 4 *<p> Company: xxx</p> 5 * 6 *@author Jason 7 *@Version 1.0 2018/12/27 16:49 8 */ 9 @SpringBootTest 10 public class DateTest { 11 12 /** 13 * 計算連個日期之間相差的天數 14 */ 15 @Test 16 public void testBetweenDays(){ 17 // 日期字符串 18 String dateStr1 = "2018-12-27 17:07:07"; 19 String dateStr2 = "2018-12-31 00:00:00"; 20 21 // 獲取日期 22 Date date1 = MyDateUtil.parseDate(dateStr1, "yyyy-MM-dd HH:mm:ss"); 23 Date date2 = MyDateUtil.parseDate(dateStr2, "yyyy-MM-dd HH:mm:ss"); 24 25 // 獲取相差的天數 26 Calendar calendar = Calendar.getInstance(); 27 calendar.setTime(date1); 28 long timeInMillis1 = calendar.getTimeInMillis(); 29 calendar.setTime(date2); 30 long timeInMillis2 = calendar.getTimeInMillis(); 31 32 long betweenDays = (timeInMillis2 - timeInMillis1) / (1000L*3600L*24L); 33 System.out.println(betweenDays); 34 } 35 36 /** 37 * 計算連個日期之間相差的天數 38 */ 39 @Test 40 public void testBetweenDays2(){ 41 // 日期字符串 42 String dateStr1 = "2018-12-27 17:07:07"; 43 String dateStr2 = "2018-12-31 00:00:00"; 44 45 // 獲取日期 46 Date date1 = MyDateUtil.parseDate(dateStr1, "yyyy-MM-dd HH:mm:ss"); 47 Date date2 = MyDateUtil.parseDate(dateStr2, "yyyy-MM-dd HH:mm:ss"); 48 49 long betweenDays = (date2.getTime() - date1.getTime()) / (1000L*3600L*24L); 50 System.out.println(betweenDays); 51 } 52 53 }