//獲取當前時間 LocalDateTime d0 = LocalDateTime.now(); System.out.println(DataConvertUtil.localDateTimeToStr(d0, "yyyy-MM-dd HH:mm:ss")); //輸入年月日時分秒實例化新的時間對象 LocalDateTime d1 = LocalDateTime.of(2017, 5, 15, 20, 32, 12); System.out.println(DataConvertUtil.localDateTimeToStr(d1, "yyyy-MM-dd HH:mm:ss")); //對一個時間對象加5分鐘(同理也可加/減,年/月/日/時/分/秒),加是plus,減是minus LocalDateTime d2 = d1.plusMinutes(5); System.out.println(DataConvertUtil.localDateTimeToStr(d2, "yyyy-MM-dd HH:mm:ss")); //對一個時間對象單獨設置年月日時分秒 //把小時設爲5點 LocalDateTime d21 = d2.withHour(5); //把小時設爲5點,0分,0秒 d21 = d2.withHour(6).withMinute(0).withSecond(0); System.out.println(DataConvertUtil.localDateTimeToStr(d21, "yyyy-MM-dd HH:mm:ss")); //獲取日(同理也可獲取年月日時分秒) int day = d2.getDayOfMonth(); System.out.println(day); //判斷兩個時間的前後,能夠用isAfter或isBefore,以下例就是判斷d2是否晚於d1 boolean b1 = d2.isAfter(d1); System.out.println(b1); //兩個時間相減,獲取時間差(例子爲d2-d1,並獲取時間差(單位:分鐘)) //相似Duration的還有Period,Period專用於計算年月日 Duration duration = Duration.between(d1, d2); long m1 = duration.toMinutes(); System.out.println(m1); //Date和LocalDateTime互轉 Date dx1 = DataConvertUtil.localDateTimeToDate(d1); LocalDateTime d3 = DataConvertUtil.dateToLocalDateTime(dx1); System.out.println(DataConvertUtil.localDateTimeToStr(d3, "yyyy-MM-dd HH:mm:ss")); //Date和LocalDate互轉 LocalDate d31 = LocalDate.now(); dx1 = DataConvertUtil.localDateToDate(d31); d31 = DataConvertUtil.dateToLocalDate(dx1); //LocalDateTime按指定格式輸出string //還有可指定輸出格式版本localDateTimeToStr(LocalDateTime value, String format) String s1 = DataConvertUtil.localDateTimeToStr(d1, "yyyy-MM-dd HH:mm:ss"); System.out.println("s1=" + s1); //string按指定格式轉爲LocalDateTime //還有可指定格式版本strToLocalDateTime(String value, String format) LocalDateTime d4 = DataConvertUtil.strToLocalDateTime(s1, "yyyy-MM-dd HH:mm:ss"); System.out.println(DataConvertUtil.localDateTimeToStr(d4, "yyyy-MM-dd HH:mm:ss")); //注意!!!!! //因爲LocalDateTime類型的限制,字符串轉LocalDateTime時字符串值必須完整包含年月日時分秒,而實際狀況常常會只有年月日 //對於這種狀況,能夠用下例的DataConvertUtil.strToLocalDate轉爲LocalDate類型,而後再把LocalDate轉爲LocalDateTime String s2 = DataConvertUtil.localDateTimeToStr(d1, "yyyy-MM-dd"); //字符串轉LocalDate LocalDate d5 = DataConvertUtil.strToLocalDate(s2, "yyyy-MM-dd"); System.out.println(DataConvertUtil.localDateToStr(d5)); //LocalDate轉LocalDateTime LocalDateTime d6 = LocalDateTime.of(d5, LocalTime.of(0, 0)); System.out.println(DataConvertUtil.localDateTimeToStr(d6, "yyyy-MM-dd HH:mm:ss")); //如下補充DataConvertUtil類的相關函數 /** * LocalDateTime轉Date * * @param localDateTime * @return */ public static Date localDateTimeToDate(LocalDateTime localDateTime) { if (localDateTime == null) return null; return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()); } /** * Date轉LocalDateTime * * @param date * @return */ public static LocalDateTime dateToLocalDateTime(Date date) { if (date == null) return null; return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); } /** * LocalDate轉Date * * @param localDate * @return */ public static Date localDateToDate(LocalDate localDate) { if (localDate == null) return null; return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()); } /** * Date轉LocalDate * * @param date * @return */ public static LocalDate dateToLocalDate(Date date) { if (date == null) return null; return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); } /** * 日期時間(LocalDateTime)按默認格式轉字符串 * * @param value * @return */ public static String localDateTimeToStr(LocalDateTime value) { return localDateTimeToStr(value, "yyyy-MM-dd"); } /** * 日期時間(LocalDateTime)按指定格式轉字符串 * * @param value * @param format * @return */ public static String localDateTimeToStr(LocalDateTime value, String format) { String dateString; if (value == null) { dateString = ""; } else { DateTimeFormatter formatDate = DateTimeFormatter.ofPattern(format); dateString = value.format(formatDate); } return dateString; } /** * 字符串按默認格式轉日期時間(LocalDateTime) * * @param value * @return */ public static LocalDateTime strToLocalDateTime(String value) { return strToLocalDateTime(value, "yyyy-MM-dd HH:mm:ss"); } /** * 字符串按指定格式轉日期時間(LocalDateTime) * * @param value * @param format * @return */ public static LocalDateTime strToLocalDateTime(String value, String format) { if (value != null && value.trim().length() > 0) { DateTimeFormatter formatDate = DateTimeFormatter.ofPattern(format); try { return LocalDateTime.parse(value, formatDate); } catch (Exception e) { return null; } } return null; }