
上篇已经讲过时间日期类 的基础用法,这篇讲的是时间日期类的高级用法

public class JDK8DateDemo2 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
LocalDateTime localDateTime = LocalDateTime.of(2020, 11, 11, 11, 11, 11);
System.out.println(localDateTime);
}
}
public class JDK8DateDemo3 {
public static void main(String[] args) {
LocalDateTime localDateTime = LocalDateTime.of(2020, 11, 11, 11, 11, 20);
//public int getYear() 获取年
int year = localDateTime.getYear();
System.out.println("年为" +year);
//public int getMonthValue() 获取月份(1-12)
int month = localDateTime.getMonthValue();
System.out.println("月份为" + month);
Month month1 = localDateTime.getMonth();
// System.out.println(month1);
//public int getDayOfMonth() 获取月份中的第几天(1-31)
int day = localDateTime.getDayOfMonth();
System.out.println("日期为" + day);
//public int getDayOfYear() 获取一年中的第几天(1-366)
int dayOfYear = localDateTime.getDayOfYear();
System.out.println("这是一年中的第" + dayOfYear + "天");
//public DayOfWeek getDayOfWeek()获取星期
DayOfWeek dayOfWeek = localDateTime.getDayOfWeek();
System.out.println("星期为" + dayOfWeek);
//public int getMinute() 获取分钟
int minute = localDateTime.getMinute();
System.out.println("分钟为" + minute);
//public int getHour() 获取小时
int hour = localDateTime.getHour();
System.out.println("小时为" + hour);
}
}

public class JDK8DateDemo4 {
public static void main(String[] args) {
LocalDateTime localDateTime = LocalDateTime.of(2020, 12, 12, 8, 10, 12);
//public LocalDate toLocalDate () 转换成为一个LocalDate对象
LocalDate localDate = localDateTime.toLocalDate();
System.out.println(localDate);
//public LocalTime toLocalTime () 转换成为一个LocalTime对象
LocalTime localTime = localDateTime.toLocalTime();
System.out.println(localTime);
}
}
public class JDK8DateDemo5 {
public static void main(String[] args) {
//method1();
//method2();
}
private static void method2() {
//public static LocalDateTime parse (准备解析的字符串, 解析格式) 把一个日期字符串解析成为一个LocalDateTime对象
String s = "2020年11月12日 13:14:15";
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
LocalDateTime parse = LocalDateTime.parse(s, pattern);
System.out.println(parse);
}
private static void method1() {
LocalDateTime localDateTime = LocalDateTime.of(2020, 11, 12, 13, 14, 15);
System.out.println(localDateTime);
//public String format (指定格式) 把一个LocalDateTime格式化成为一个字符串
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
String s = localDateTime.format(pattern);
System.out.println(s);
}
}
/**
* JDK8 时间类添加或者减去时间的方法
*/
public class JDK8DateDemo6 {
public static void main(String[] args) {
//public LocalDateTime plusYears (long years) 添加或者减去年
LocalDateTime localDateTime = LocalDateTime.of(2020, 11, 11, 13, 14, 15);
//LocalDateTime newLocalDateTime = localDateTime.plusYears(1);
//System.out.println(newLocalDateTime);
LocalDateTime newLocalDateTime = localDateTime.plusYears(-1);
System.out.println(newLocalDateTime);
}
}

/**
* JDK8 时间类减少或者添加时间的方法
*/
public class JDK8DateDemo7 {
public static void main(String[] args) {
//public LocalDateTime minusYears (long years) 减去或者添加年
LocalDateTime localDateTime = LocalDateTime.of(2020, 11, 11, 13, 14, 15);
//LocalDateTime newLocalDateTime = localDateTime.minusYears(1);
//System.out.println(newLocalDateTime);
LocalDateTime newLocalDateTime = localDateTime.minusYears(-1);
System.out.println(newLocalDateTime);
}
}

/**
* JDK8 时间类修改时间
*/
public class JDK8DateDemo8 {
public static void main(String[] args) {
//public LocalDateTime withYear(int year) 修改年
LocalDateTime localDateTime = LocalDateTime.of(2020, 11, 11, 13, 14, 15);
// LocalDateTime newLocalDateTime = localDateTime.withYear(2048);
// System.out.println(newLocalDateTime);
LocalDateTime newLocalDateTime = localDateTime.withMonth(20);
System.out.println(newLocalDateTime);
}
}

/**
* 计算两个时间的间隔
*/
public class JDK8DateDemo9 {
public static void main(String[] args) {
//public static Period between(开始时间,结束时间) 计算两个"时间"的间隔
LocalDate localDate1 = LocalDate.of(2020, 1, 1);
LocalDate localDate2 = LocalDate.of(2048, 12, 12);
Period period = Period.between(localDate1, localDate2);
System.out.println(period);//P28Y11M11D
//public int getYears() 获得这段时间的年数
System.out.println(period.getYears());//28
//public int getMonths() 获得此期间的月数
System.out.println(period.getMonths());//11
//public int getDays() 获得此期间的天数
System.out.println(period.getDays());//11
//public long toTotalMonths() 获取此期间的总月数
System.out.println(period.toTotalMonths());//347
}
}
/**
* 计算两个时间的间隔
*/
public class JDK8DateDemo10 {
public static void main(String[] args) {
//public static Duration between(开始时间,结束时间) 计算两个“时间"的间隔
LocalDateTime localDateTime1 = LocalDateTime.of(2020, 1, 1, 13, 14, 15);
LocalDateTime localDateTime2 = LocalDateTime.of(2020, 1, 2, 11, 12, 13);
Duration duration = Duration.between(localDateTime1, localDateTime2);
System.out.println(duration);//PT21H57M58S
//public long toSeconds() 获得此时间间隔的秒
System.out.println(duration.toSeconds());//79078
//public int toMillis() 获得此时间间隔的毫秒
System.out.println(duration.toMillis());//79078000
//public int toNanos() 获得此时间间隔的纳秒
System.out.println(duration.toNanos());//79078000000000
}
}