package com.date; import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.MonthDay; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; import java.util.Date; import org.junit.Test; public class DateAllMethods { //测试LocalDate类★ ★★★★★★★★★★★★★★★★★★★★★★★★ @Test public void testLocalDate() { // 获取当前日期(只包含日期,不包含时间)第三代 //只有日期 LocalDate date = LocalDate.now(); System.out.println(date); // 获取日期的指定部分 System.out.println("year:"+date.getYear()); System.out.println("month:"+date.getMonth()); System.out.println("day:"+date.getDayOfMonth()); System.out.println("week:"+date.getDayOfWeek()); // 根据指定的日期参数,创建LocalDate对象 LocalDate of = LocalDate.of(2010, 3, 2); //可以使用年月日来反向生成当前的日期对象 System.out.println(of); } // 测试LocalTime类 @Test public void testLocalTime() { // 获取当前时间(只包含时间,不包含日期) LocalTime time = LocalTime.now(); System.out.println(time); // 获取时间的指定部分 System.out.println("hour:" + time.getHour()); System.out.println("minute:" + time.getMinute()); System.out.println("second:" + time.getSecond()); System.out.println("nano:" + time.getNano()); // 根据指定的时间参数,创建LocalTime对象 LocalTime of = LocalTime.of(10, 20, 55); System.out.println(of); } // 测试LocalDateTime类 @Test public void testLocalDateTime() { // 获取当前时间(包含时间+日期) LocalDateTime time = LocalDateTime.now(); // 获取时间的指定部分 System.out.println("year:" + time.getYear()); System.out.println("month:" + time.getMonthValue()); System.out.println("day:" + time.getMonth()); System.out.println("day:" + time.getDayOfMonth()); System.out.println("hour:" + time.getHour()); System.out.println("minute:" + time.getMinute()); System.out.println("second:" + time.getSecond()); System.out.println("nano:" + time.getNano()); // 根据指定的时间参数,创建LocalTime对象 LocalDateTime of = LocalDateTime.of(2020, 2, 2, 10, 20, 55); System.out.println(of); } // 测试MonthDay类:检查重复事件 @Test public void testMonthDay() { LocalDate birth = LocalDate.of(1994, 12, 12); MonthDay birthMonthDay = MonthDay.of(birth.getMonthValue(), birth.getDayOfMonth()); LocalDate now = LocalDate.now();//当前日期 MonthDay current = MonthDay.from(now); //通过LocalDate 获取月日 System.out.println(current); //我们的日期是相互可以比较的 if (birthMonthDay.equals(current)) { System.out.println("今天生日"); } else { System.out.println("今天不生日"); } } // 测试是否是闰年 ★ ★★★★★★★★★★★★★★★★★★★★★★★★ @Test public void testIsLeapYear() { LocalDate now = LocalDate.now();//当前日期 System.out.println(now.isLeapYear());//判断闰年 } // 测试增加日期的某个部分 @Test public void testPlusDate() { LocalDate now = LocalDate.now(); //查看三年前今天的日期 LocalDate plusYears = now.plusYears(-3); //这里很灵活 //LocalDate plusYears = now.plusWeeks(-3); //查看三周前的日期 System.out.println(plusYears); } // 使用plus方法测试增加时间的某个部分 @Test public void testPlusTime() { LocalTime now = LocalTime.now(); //查看一小时后的时间 LocalTime plusHours = now.plusHours(1); System.out.println(plusHours); } // 使用minus方法测试查看一年前和一年后的日期 @SuppressWarnings("unused") @Test public void testMinusTime() { LocalDate now = LocalDate.now(); //在当前日期基础上减去1年 LocalDate minus = now.minus(1, ChronoUnit.YEARS); LocalDate minus2 = now.minusYears(1); System.out.println(minus2); } //测试时间戳类:Instant ,相当于以前的Date类 ★ ★★★★★★★★★★★★★★★★★★★★★★★★ @Test public void testInstant() { Instant now = Instant.now(); System.out.println(now); //与Date类的转换 Date date = Date.from(now); System.out.println(date); Instant instant = date.toInstant(); System.out.println(instant); } //测试DateTimeFormatter ★ ★★★★★★★★★★★★★★★★★★★★★★★★ @Test public void testDateTimeFormatter() { //第三代 DateTimeFormatter //可以将 日期转换成字符串 DateTimeFormatter pattern = DateTimeFormatter.ofPattern("MM-dd yyyy HH:mm:ss"); //将字符串转换成日期 LocalDateTime parse = LocalDateTime.parse("03-03 2017 08:40:50", pattern); System.out.println(parse); //将日期转换成字符串 String format = pattern.format(parse); System.out.println(format); } }
来源:https://www.cnblogs.com/yonxin/p/12490462.html