@RunWith(SpringRunner.class)
@SpringBootTest
public class DataTimeUtil {
@Test
public void test(){
LocalDateTime local=LocalDateTime.now();
System.out.println("获取默认当前时间为 "+local);
Clock clock=Clock.systemDefaultZone();
System.out.println("获取默认当前的Clock "+clock.toString());
LocalDateTime localDateTime1=LocalDateTime.now(clock);
System.out.println("获取Clock时区的时间为"+localDateTime1);
LocalDate localDate= LocalDate.now();
LocalTime localTime=LocalTime.now();
LocalDateTime localDateTime2=LocalDateTime.of(localDate,localTime);
System.out.println("localDate是 "+localDate+" localTime是 "+localTime+" 获取的localDateTime2 "+localDateTime2);
System.out.println("分割线-------------------------------------------------------------------分割线");
Integer dayOfYear=local.getDayOfYear();
Integer dayOfMonth=local.getDayOfMonth();
DayOfWeek dayOfWeek=local.getDayOfWeek();
System.out.println("获得是年份中的第几天 "+dayOfYear+" 获取月份中的第几天 "+dayOfMonth+" 获得整个星期的星期几 "+dayOfWeek);
Integer year=local.getYear();
Month month=local.getMonth();
Integer hour=local.getHour();
Integer minute=local.getMinute();
Integer second=local.getSecond();
System.out.println("获得年份 "+year+" 获得月份 "+month+" 获得小时 "+hour+" 获得分钟 "+minute+ " 获得秒钟 "+second);
System.out.println("分割线-------------------------------------------------------------------分割线");
LocalDateTime currentTimeWithDayOfYear=local.withDayOfYear(12);
LocalDateTime currentTimeWithDayOfMonth=local.withDayOfMonth(11);
LocalDateTime currentTimeWithYear=local.withYear(2019);
LocalDateTime currentTimeWithMonth=local.withMonth(1);
LocalDateTime currentTimeWithHour=local.withHour(12);
LocalDateTime currentTimeWithMinute=local.withMinute(22);
LocalDateTime currentTimeWithSecond=local.withSecond(12);
System.out.println("年份中天变更 "+currentTimeWithDayOfYear+" 月份中天变更 "+currentTimeWithDayOfMonth+" 年份变更 "+currentTimeWithYear
+"月份变更 "+currentTimeWithMonth+" 时变更 "+currentTimeWithHour+" 分钟变更 "+currentTimeWithMinute
+"秒变更 "+currentTimeWithSecond);
System.out.println("分割线-------------------------------------------------------------------分割线");
LocalDateTime currentTimeAddYear=local.plusYears(1);
LocalDateTime currentTimeAddMonth=local.plusMonths(1);
LocalDateTime currentTimeAddDay=local.plusDays(1);
LocalDateTime currentTimeAddWeek=local.plusWeeks(1);
LocalDateTime currentTimeAddHour=local.plusHours(1);
LocalDateTime currentTimeAddSecond=local.plusSeconds(1);
LocalDateTime currentTimeAddMinutes=local.plusMinutes(1);
System.out.println("加年份 "+currentTimeAddYear+" 加月份 "+currentTimeAddMonth+" 加天 "+currentTimeAddDay
+" 加周 "+currentTimeAddWeek+" 加小时 "+currentTimeAddHour+" 加分钟 "+currentTimeAddMinutes
+" 加秒 "+currentTimeAddSecond);
LocalDateTime currentTimeMinusYear=local.minusYears(1);
LocalDateTime currentTimeMinusMonth=local.minusMonths(1);
LocalDateTime currentTimeMinusWeek=local.minusWeeks(1);
LocalDateTime currentTimeMinusDay=local.minusDays(1);
LocalDateTime currentTimeMinusHour=local.minusHours(1);
LocalDateTime currentTimeMinusMinute=local.minusMinutes(1);
LocalDateTime currentTimeMinusSecond=local.minusSeconds(1);
System.out.println("减年份 "+currentTimeMinusYear+" 减月份 "+currentTimeMinusMonth+" 减天 "+currentTimeMinusDay
+" 减周 "+currentTimeMinusWeek+" 减小时 "+currentTimeMinusHour+" 减分钟 "+currentTimeMinusMinute
+" 减秒 "+currentTimeMinusSecond);
System.out.println("分割线-------------------------------------------------------------------分割线");
DateTimeFormatter formatter=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//它会把时间格式化为指定格式的字符串 创建实例才能使用format()方法
String nowTime=local.format(formatter);
System.out.println("格式化后的数据时(得到String类型) "+nowTime);
//parse()方法通过LocalDateTime
String str11="2007-12-03T10:15:30";
//注意当String str="2010-1-1 10:10:10"格式会报错
String str="2010-01-01 10:10:10";
LocalDateTime parseTime=LocalDateTime.parse(str11);
LocalDateTime parseTime1=LocalDateTime.parse(str,formatter);
System.out.println("格式化后的数据时(得到LocalDateTime类型) "+ parseTime+
"格式化后的数据时(得到LocalDateTime类型)有俩个参数 "+parseTime1);
//传入的参数是LocalDateTime类型
LocalDateTime date=parseTime;
boolean flag=local.isBefore(date);
boolean flag1=local.isAfter(date);
System.out.println("传入的参数是 "+date+" 是否在传入的数据之前 "+flag+"是否在传入的数据之后 "+flag1);
//是否相等
boolean flag2=local.equals(date);
System.out.println("localDateTime1和localDateTime2是否相等 "+flag2);
boolean flag3=local.isEqual(date);
System.out.println("localDateTime1和localDateTime2是否相等 "+flag3);
String Str=date.toString();
System.out.println("输出字符串 "+Str);
}
}