一:DateTimeFormatter
(1)简介:它用于时间的格式化和解析
- static DateTimeFormatter ofPattern(格式) 获取格式对象
- String format(时间对象) 按照指定方式格式化
(2)案例演示
演示代码截图如下:
运行结果如下图所示:
二:LocalDate
(1)简介:用于获取年、月、日
(2)案例演示1:
案例演示截图如下:
运行结果截图如下:
案例演示2:判断今天是否是国庆节
案例演示截图如下:
运行结果如下图所示:
三:LocalTime
(1)简介:获取时分秒
(2) 案例演示:
演示截图如下:
打印结果如下:
四。案例演示代码
案例演示所有代码如下:
/*
static DateTimeFormatter ofPattern(格式) 获取格式对象
String format(时间对象) 按照指定方式格式化
*/
//获取时间的对象
//细节
//Instant.now() 表示利用方法获取当前的Instant对象
//atZone() 利用Instant里面的atZone方法来指定时区
//ZoneId.of("Asia/Shanghai") 利用ZoneId里面of方法来获取指定时区,即将获取的时区放到atZone里面为指定时区
ZonedDateTime time = Instant.now().atZone(ZoneId.of("Asia/Karachi"));
//解析/格式化
DateTimeFormatter sdf1 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH时mm分ss秒; EE s");
//格式化
System.out.println(sdf1.format(time));
}
//1.获取当前时间的日历对象(包含 年月日)
LocalDate nowDate = LocalDate.now();
System.out.println("今天的日期:" + nowDate);
System.out.println("----------------------------------");
//2.获取指定日期的日期对象
LocalDate ldDate = LocalDate.of(2023,9,1);
System.out.println("指定日期:" + ldDate);//指定日期:2023-09-01
System.out.println("------------------------------------");
//3.get系列方法获取日历的每一个属性值
//获取年
int year = ldDate.getYear();
System.out.println("year:" + year);//year:2023
System.out.println("-------------------------------------");
//4.获取月份
//方式一:
Month m = ldDate.getMonth();
System.out.println("month:" + m);//SEPTEMBER
System.out.println(m.getValue());
//方示二:
int month = ldDate.getMonthValue();
System.out.println("month:" + month);
System.out.println("-----------------------------------");
//获取日
int day = ldDate.getDayOfMonth();
System.out.println("day:" + day);
//获取第一年中的第几天
int dayOfYear = ldDate.getDayOfYear();
System.out.println(dayOfYear);
//获取星期
DayOfWeek dayOfWeek = ldDate.getDayOfWeek();
System.out.println(dayOfWeek);
System.out.println(dayOfWeek.getValue());
//is开头的方法表示判断
System.out.println(ldDate.isBefore(ldDate));
System.out.println(ldDate.isAfter(ldDate));
//with开头的方法表示修改,只能修改年月日
LocalDate withLocalDate = ldDate.withYear(2005);
System.out.println(withLocalDate);
System.out.println(withLocalDate == ldDate);//false
//minus开头的方法表示减少,只能减少年月日
LocalDate minusLocalDate = ldDate.minusYears(2);
System.out.println(minusLocalDate);
//plus开头的方法表示增加,只能增加年月日
LocalDate plusLocalDate = ldDate.plusDays(5);
System.out.println(plusLocalDate);
//需求1:判断今天是否是国庆节?
//指定国庆节
LocalDate NationNalDay = LocalDate.of(1949,10,1);
//获取今天的日期
LocalDate nowDate1 = LocalDate.now();
//获取国庆节的具体日期
MonthDay NationNalDayMd = MonthDay.of(NationNalDay.getMonthValue(),NationNalDay.getDayOfMonth());
MonthDay nowMd = MonthDay.from(nowDate1);
//打印比较时间看今天是否是国庆节
System.out.println("今天是国庆节吗? " + NationNalDayMd.equals(nowMd));
public static void main(String[] args){
//获取本地时间的日历对象。(包含时、分、秒)
LocalTime nowTime = LocalTime.now();
System.out.println("今天的时间:" + nowTime);
int hour = nowTime.getHour();//时
System.out.println("hour:" + hour);
int minutes = nowTime.getMinute();//分
System.out.println("minute:" + minutes);
int second = nowTime.getSecond(); //秒
System.out.println("second" + second);
int nano = nowTime.getNano();//纳秒
System.out.println("nano:" + nano);
System.out.println("----------------------");
System.out.println(LocalTime.of(8,20));//时分
System.out.println(LocalTime.of(8,20,30));//时分秒
System.out.println(LocalTime.of(8,20,30,650));//时分秒纳秒
LocalTime mTime = LocalTime.of(8,20,30,750);
//is系列的方法
System.out.println(nowTime.isBefore(mTime));
System.out.println(nowTime.isAfter(mTime));
//with系列的方法,只能修改时、分、秒
System.out.println(nowTime.withHour(10));
//minus系列方法,只能修改时、分、秒
System.out.println(nowTime.minusHours(10));
//plus系列的方法,只能修改时、分、秒
System.out.println(nowTime.plusHours(10));