1 获取当前时间
@Test
public void now(){
LocalDateTime now = LocalDateTime.now();
LocalDate now1 = LocalDate.now();
LocalTime now2 = LocalTime.now();
System.out.println(now);
System.out.println(now1);
System.out.println(now2);
}
2 获取当前时间的年月日时分秒
@Test
public void now1(){
LocalDateTime now = LocalDateTime.now();
int year = now.getYear();
int month = now.getMonthValue();
int day = now.getDayOfMonth();
int hour = now.getHour();
int minute = now.getMinute();
int second = now.getSecond();
System.out.println(year);
System.out.println(month);
System.out.println(day);
System.out.println(hour);
System.out.println(minute);
System.out.println(second);
}
3 在当前时间基础上加上对应的时间
@Test
public void now2(){
LocalDateTime now = LocalDateTime.now();
LocalDateTime now1 = now.plusYears(5);
LocalDateTime now2 = now.plusMonths(5);
LocalDateTime now3 = now.plusDays(7);
LocalDateTime now4 = now.plusHours(2);
LocalDateTime now5 = now.plusMinutes(30);
LocalDateTime now6 = now.plusSeconds(30);
System.out.println(now);
System.out.println(now1);
System.out.println(now2);
System.out.println(now3);
System.out.println(now4);
System.out.println(now5);
System.out.println(now6);
}
4 在当前时间基础上减去对应的时间
@Test
public void now3(){
LocalDateTime now = LocalDateTime.now();
LocalDateTime now1 = now.minusYears(5);
LocalDateTime now2 = now.minusMonths(5);
LocalDateTime now3 = now.minusDays(7);
LocalDateTime now4 = now.minusHours(2);
LocalDateTime now5 = now.minusMinutes(30);
LocalDateTime now6 = now.minusSeconds(30);
System.out.println(now);
System.out.println(now1);
System.out.println(now2);
System.out.println(now3);
System.out.println(now4);
System.out.println(now5);
System.out.println(now6);
}
5 改变当前时间(年月日时分秒)
@Test
public void now4(){
LocalDateTime now = LocalDateTime.now();
LocalDateTime now1 = now.withYear(2060);
LocalDateTime now2 = now.withMonth(12);
LocalDateTime now3 = now.withDayOfMonth(29);
LocalDateTime now4 = now.withHour(23);
LocalDateTime now5 = now.withMinute(30);
LocalDateTime now6 = now.withSecond(23);
LocalDateTime now7 = now.withDayOfYear(60);
System.out.println(now);
System.out.println(now1);
System.out.println(now2);
System.out.println(now3);
System.out.println(now4);
System.out.println(now5);
System.out.println(now6);
System.out.println(now7);
}
6 两个时间作比较
@Test
public void compareTo(){
LocalDateTime now = LocalDateTime.now();
LocalDateTime now1 = now.plusYears(5);
LocalDateTime of = LocalDateTime.of(2022,2,5,1,1,1);
LocalDateTime of1 = LocalDateTime.of(2022,8,5,1,1,1);
int compareTo = now1.compareTo(now);
int compareTo1 = now.compareTo(now1);
int compareTo2 = now.compareTo(of);
int compareTo3 = now.compareTo(of1);
System.out.println(now);
System.out.println(now1);
System.out.println(of);
System.out.println(of1);
System.out.println(compareTo);
System.out.println(compareTo1);
System.out.println(compareTo2);
System.out.println(compareTo3);
}
7 给LocalDateTime 赋值
@Test
public void ofParse(){
LocalDateTime of = LocalDateTime.of(2022,2,5,1,1,1);
System.out.println(of);
LocalDateTime parse = LocalDateTime.parse("2022-02-05 01:01:01", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println(parse);
}
8 时间转换
@Test
public void ofPattern() {
String dateTime = "2021-01-05 12:00:00";
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime ldt = LocalDateTime.parse(dateTime, df);
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter of = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String dateTime1 = now.format(of);
System.out.println(ldt);
System.out.println(dateTime1);
}
9 计算时间差
@Test
public void duration(){
LocalDateTime now = LocalDateTime.now();
LocalDateTime of = LocalDateTime.of(2022,7,13,1,1,1);
Duration duration = Duration.between(of,now);
long days = duration.toDays();
long hours = duration.toHours();
long minutes = duration.toMinutes();
long millis = duration.toMillis();
long nanos = duration.toNanos();
System.out.println(days);
System.out.println(hours);
System.out.println(minutes);
System.out.println(millis);
System.out.println(nanos);
}
Date currentTime = new Date();
ZoneId zoneId = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(currentTime.toInstant(), zoneId);
LocalDateTime tomorrowTime = LocalDateTime.ofInstant(currentTime.toInstant(), zoneId)
.plusDays(1).withHour(23).withMinute(59).withSecond(59).withNano(999);
long seconds = ChronoUnit.SECONDS.between(localDateTime, tomorrowTime);
System.out.println(seconds);
long minutes = ChronoUnit.MINUTES.between(localDateTime, tomorrowTime);
System.out.println(minutes);
long hours = ChronoUnit.HOURS.between(localDateTime, tomorrowTime);
System.out.println(hours);