核心代码一
import java.time.LocalDate;
public class DateFormat1 {
public static void main(String[] args) throws Exception {
LocalDate now = LocalDate.now();
System.out.println("今天日期:" + now);
System.out.println("年:"+now.getYear());
System.out.println("月份:" + now.getMonth());
System.out.println("月份(数字):" + now.getMonthValue());
System.out.println("几号:" + now.getDayOfMonth());
System.out.println("周几:" + now.getDayOfWeek());
LocalDate localDate = now.plusYears(1);
System.out.println("加后是哪一年:" + localDate.getYear());
System.out.println("原来的是哪一年:" + now .getYear());
System.out.println("isAfter(是否在某一年之后):" + localDate.isAfter(now));
}
}
核心代码二
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class DateFormat2 {
public static void main(String[] args) throws Exception {
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime);
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format = dateTimeFormatter.format(localDateTime);
System.out.println("格式化时间:" + format);
DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
String format2 = dateTimeFormatter2.format(localDateTime);
System.out.println("格式化时间:" + format2);
LocalDateTime of = LocalDateTime.of(2020, 10, 10, 10, 20, 30);
System.out.println("构造的时间:" + of);
System.out.println("计算时间差----------------------");
LocalDateTime localDateTime1 = LocalDateTime.now();
System.out.println(localDateTime1);
LocalDateTime localDateTime2 = LocalDateTime.of(2020, 10, 10, 10, 20, 30);
System.out.println(localDateTime2);
Duration between = Duration.between(localDateTime2, localDateTime1);
System.out.println("相差天数:" + between.toDays());
System.out.println("相差小时:" + between.toHours());
System.out.println("相差分钟:" + between.toMinutes());
System.out.println("相差毫秒:" + between.toMillis());
System.out.println("相差纳秒数:" + between.toNanos());
}
}