日期类--LocalDate、LocalTime、LocalDateTime(API)

159 阅读2分钟
小案例
```
 String startTime = "2022-11-28 00:00:00";
 String endTime = "2022-11-28 00:10:00";

 String XiaoJia = "2022-11-28 00:04:50";
 String XiaoPi = "2022-11-28 00:10:01";

 SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
Date d1 = sdf.parse(startTime); //parse将字符串转换成时间对象
Date d2 = sdf.parse(endTime);
Date d3 = sdf.parse(XiaoJia);
Date d4 = sdf.parse(XiaoPi);

if(d3.after(d1) && d3.before(d2)){  //after 在...时间之前 before 在....时间之后
    System.out.println("小贾秒杀成功");
}else{
    System.out.println("小贾秒杀失败");
}
if(d4.after(d1)&&d4.before(d2)){
    System.out.println("小皮秒杀成功");
}else {
    System.out.println("小皮秒杀失败");
}
```

LocalDate、LocalTime、LocalDateTime

 LocalDate nowDate = LocalDate.now();//LocalDate.now(); 获取年月日时间对象 只包含 年 月 日
 System.out.println(nowDate);//直接输出得到当前时间 年 月 日

 int year = nowDate.getYear();//getYear(); 获取当前年
 System.out.println(year + "年");

 int month = nowDate.getMonthValue();//getMonthValue(); 获取当前月
 System.out.println(month + "月");

 int day = nowDate.getDayOfMonth();//getDayOfMonth(); 获取当前日
 System.out.println(day+"日");

 int dayOfYear = nowDate.getDayOfYear();//getDayOfYear(); 获取当年的第几天
 System.out.println("今年已经过去了"+dayOfYear+"天");

 DayOfWeek weekOfEnglish = nowDate.getDayOfWeek();//getDayOfWeek(); 获取英文的星期
 int week = nowDate.getDayOfWeek().getValue();//getDayOfWeek().getValue(); 支持链式编程加上.getValue();获取数字的星期
 System.out.println("today is " + weekOfEnglish);
 System.out.println("今天星期:"+week);

 System.out.println(nowDate.getMonth());//getMonth(); 获取英文的月
 System.out.println(nowDate.getMonth().getValue()); //getValue(); 获取数字的月

 LocalDate bt = LocalDate.of(2005,11,28);//获取指定的一天
 System.out.println("My girlfriend's birthday is "+bt);
 System.out.println(LocalDate.of(2005, Month.NOVEMBER, 28));//支持枚举


 LocalTime nowTime = LocalTime.now();//LocalTime.now(); 获取当前 时 分 秒对象 只包含 时 分 秒
 System.out.println("现在时间是" + nowTime);//直接输出得到 时 分 秒

 int hour = nowTime.getHour();//getHour(); 获取小时
 System.out.print("现在时间是" + hour + "点 ");

 int minute = nowTime.getMinute();//getMinute(); 获取分钟
 System.out.print(minute + "分 ");

 int second = nowTime.getSecond();//getSecond(); 获取秒钟
 System.out.println(second + "秒");

 int nano = nowTime.getNano();//getNano(); 获取纳秒
 System.out.println(nano);

 System.out.println(LocalTime.of(11,22));//指定获取 时 分
 System.out.println(LocalTime.of(11, 22, 13));//指定获取 时 分 秒
 System.out.println(LocalTime.of(11, 22, 13, 150));//指定获取 时 分 秒 纳秒
LocalTime localTime = LocalTime.of(11,22,13,150);//赋值当前指定的时间
 System.out.println(localTime);

 System.out.println(LocalDateTime.of(2005, 11, 28, 0, 0, 0, 0));
 //LocalDateTime.of(); 指定获取 年 月 日 时 分 秒 纳秒 可以指定获得 时 分 秒 或者 年 月 日 非常灵活,也支持枚举

 LocalDateTime nowDateTime = LocalDateTime.now();//获取  年 月 日 时 分 秒 纳秒 的时间对象 可以理解为上面俩个对象的结合
 System.out.println("现在是" + nowDateTime);
 System.out.println(nowDateTime.getYear());//获取年
 System.out.println(nowDateTime.getHour());//获取月
 System.out.println(nowDateTime.getDayOfMonth());//获取日
 System.out.println(nowDateTime.getHour());//获取小时
 System.out.println(nowDateTime.getMinute());//获取秒
 System.out.println(nowDateTime.getSecond());//获取纳秒
 System.out.println(nowDateTime.getDayOfWeek());//获取英文星期
 System.out.println(nowDateTime.getDayOfWeek().getValue());//获取数字星期
 System.out.println(nowDateTime.getDayOfYear());//获取当年的第几天
 System.out.println(nowDateTime.getMonth());//获取英文月
 System.out.println(nowDateTime.getMonth().getValue());//获取数字月

 LocalDate ld = nowDateTime.toLocalDate();//toLocalDate(); 转换成年 月 日对象
 System.out.println(ld);
 LocalTime lt = nowDateTime.toLocalTime();//toLocalTime(); 转换成时 分 秒对象
 System.out.println(lt.getHour());//时
 System.out.println(lt.getMinute());//分
 System.out.println(lt.getSecond());//秒
 System.out.println(lt.getNano());//纳秒