SimpleDateFormat()
Date today = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
String format = df.format(today);
System.out.println(format);//20220613 16:21:41
2.获取当前日期以及格式化
//now():立即获取当前日期
LocalDate localDate = LocalDate.now();
System.out.println(localDate);//2022-06-12
//日期格式化
//DateTimeFormatter.ofPattern(): 将日期以自己想要的格式进行显示
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 E");
String formatDate = localDate.format(dateTimeFormatter);
System.out.println("格式化后:"+formatDate);//格式化后:2022年06月12日 星期日
3.把日期部分取出来
//把日期的部分取出来 int year = localDate.getYear(); int monthValue = localDate.getMonthValue(); int dayOfMonth = localDate.getDayOfMonth(); int dayOfYear = localDate.getDayOfYear(); DayOfWeek dayOfWeek = localDate.getDayOfWeek(); int week = dayOfWeek.getValue(); System.out.println(year);//2022 System.out.println(monthValue);//6 System.out.println(dayOfMonth);//12 //今年的163天 System.out.println(dayOfYear);//163 System.out.println(week);//74.自定义日期
//自定义日期
LocalDate localDate1 = LocalDate.of(2000, 8, 31);
LocalDate localDate2 = LocalDate.parse("2010-11-07");
System.out.println(localDate1.format(dateTimeFormatter));//2000年08月31日 星期四
System.out.println(localDate2.format(dateTimeFormatter));//2010年11月07日 星期日
System.out.println(localDate1);//2000-08-31
5.日期加减以及链式调用
日期加减
//日期的加减 //plus添加 LocalDate localDateTemp = localDate2.plusDays(7); System.out.println(localDateTemp.format(dateTimeFormatter));//2010年11月14日 星期日 //minus减少 localDateTemp = localDate2.minusDays(7); System.out.println(localDateTemp.format(dateTimeFormatter));//2010年10月31日 星期日 //with设置(修改某个区域,类似于set) localDateTemp = localDate2.withYear(2021); localDateTemp = localDateTemp.withMonth(8); localDateTemp = localDateTemp.withDayOfMonth(25);
链式调用
localDateTemp = localDate2.withYear(2021)
.withMonth(8)
.withDayOfMonth(25);
System.out.println(localDateTemp.format(dateTimeFormatter));//2021年08月25日 星期三
//返回值为localDate的都可链式调用
//链式调用,新建日期、添加天数、减掉月数、格式化、输出
System.out.println(LocalDate.of(2021,8,25)
.plusDays(6)
.minusMonths(2)
.format(dateTimeFormatter));//2021年06月30日 星期三



