//獲取當前時間 System.out.println(LocalDateTime.now()); //獲取當前時間時分秒 System.out.println(LocalDateTime.now().toLocalTime()); //獲取當前時間年月日 System.out.println(LocalDateTime.now().toLocalDate()); //獲取當前時間20天之前 System.out.println(LocalDateTime.now().minusDays(20)); //獲取當前時間20天之后 System.out.println(LocalDateTime.now().plusDays(20)); //獲取當前時間60天之前 System.out.println(LocalDateTime.now().minusDays(60)); //所有minus開頭都是之前 plus開頭都是之后 //比較大小 System.out.println(LocalDateTime.now().compareTo(LocalDateTime.now().plusDays(2)));
//實例化方式 以及比較大小 Duration between = Duration.between(LocalDateTime.of(1993,3,14,0,0), LocalDateTime.now()); Duration between2 = Duration.between(LocalDateTime.of(1993,3,14,0,0), LocalDateTime.now()); System.out.println(between.toDays()); System.out.println(between2.toDays());
java.time.Duration duration = java.time.Duration.between(LocalDateTime startTime, LocalDateTime endTime ); 例如: duration.toMinutes() //兩個時間差的分鍾數 toNanos()//納秒 toMillis()//毫秒 toMinutes()//分鍾 toHours()//小時 toDays()//天數
//獲取當前是第幾日 System.out.println(LocalDateTime.now().getDayOfMonth()); //獲取當前周幾 System.out.println(LocalDateTime.now().getDayOfWeek().getValue()); //獲取當前是今年第幾天 System.out.println(LocalDateTime.now().getDayOfYear());
//獲取毫秒數 Long milliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
1.獲取當前年月日的字符串
String ymd = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));