现象:
LocalDateTime API 简单使用
方法:
JDK8新特性里提供了3个时间类:LocalDate、LocalTime、LocalDateTime
一:LocalDate的使用
// 获取当前日期
LocalDate now = LocalDate.now();
System.out.println("当前时间:"+now);
//当前时间:2021-07-20
// 设置指定日期
LocalDate localDate = LocalDate.of(2021, 7, 22);
System.out.println("输出时间:"+localDate);
//输出时间:2021-07-22
// 获取年
int year = localDate.getYear();
System.out.println("获取年:"+year);
//获取年:2021
// 获取月
Month month = localDate.getMonth();
int month1 = localDate.get(ChronoField.MONTH_OF_YEAR);
System.out.println("获取月:"+month1);
//获取月:7
// 获取日
int day = localDate.getDayOfMonth();
int day1 = localDate.get(ChronoField.DAY_OF_MONTH);
System.out.println("获取日:"+day1);
//获取日:22
// 获取星期
DayOfWeek dayOfWeek = localDate.getDayOfWeek();
System.out.printf("获取星期:"+dayOfWeek);
//获取星期:THURSDAY
int dayOfWeek1 = localDate.get(ChronoField.DAY_OF_WEEK);
System.out.printf("获取星期:"+dayOfWeek1);
//获取星期:4
二:LocalTime 的使用
// 获取当前时间
LocalTime now = LocalTime.now();
System.out.println("当前时间:" + now);
//当前时间:16:43:34.748
LocalTime localTime = LocalTime.of(14, 20, 10);
System.out.println("设置时间:" + localTime);
//设置时间:14:20:10
//获取小时
int hour = localTime.getHour();
System.out.println("获取小时:" + hour);
//获取小时:14
int hour1 = localTime.get(ChronoField.HOUR_OF_DAY);
System.out.println("获取小时:" + hour1);
//获取小时:14
//获取分
int minute = localTime.getMinute();
System.out.println("获取分:" + minute);
//获取分:20
int minute1 = localTime.get(ChronoField.MINUTE_OF_HOUR);
System.out.println("获取分:" + minute1);
//获取分:20
//获取秒
int second = localTime.getSecond();
System.out.println("获取秒:" + second);
//获取秒:10
int second1 = localTime.get(ChronoField.SECOND_OF_MINUTE);
System.out.println("获取秒:" + second1);
//获取秒:10
三:LocalDateTime的使用
// 获取当前日期时间
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("当前日期时间:"+localDateTime);
//当前日期时间:2021-07-21T16:53:12.281
// 设置日期
LocalDateTime localDateTime1 = LocalDateTime.of(2021, Month.SEPTEMBER, 10, 14, 46, 56);
System.out.println("设置日期时间:"+localDateTime1);
//设置日期时间:2021-09-10T14:46:56
LocalDate localDate = LocalDate.of(2021, 7, 22);
LocalTime localTime = LocalTime.of(12, 20, 10);
LocalDateTime localDateTime2 = LocalDateTime.of(localDate, localTime);
System.out.println("设置日期时间:"+localDateTime2);
//设置日期时间:2021-07-22T12:20:10
LocalDateTime localDateTime3 = localDate.atTime(localTime);
System.out.println("日期设置时间:"+localDateTime3);
//日期设置时间:2021-07-22T12:20:10
LocalDateTime localDateTime4 = localTime.atDate(localDate);
System.out.println("时间设置日期:"+localDateTime4);
//时间设置日期:2021-07-22T12:20:10
// 获取LocalDate
LocalDate localDate2 = localDateTime.toLocalDate();
System.out.println("获取LocalDate:"+localDate2);
//获取LocalDate:2021-07-21
// 获取LocalTime
LocalTime localTime2 = localDateTime.toLocalTime();
System.out.println("获取LocalTime:"+localTime2);
//获取LocalTime:16:53:42.553
四:LocalDate、LocalTime、LocalDateTime、Instant为不可变对象,修改这些对象对象会返回一个副本。
1、增加、年数、月数、天数等使用plus方法
LocalDateTime localDateTime = LocalDateTime.of(2021, Month.JULY, 21, 14, 46, 56);
System.out.println("设置日期时间:"+localDateTime);
//当前日期时间:2021-07-21T14:46:56
//增加一年
LocalDateTime localDateTime1 = localDateTime.plusYears(1);
System.out.println(""+localDateTime1);
//2022-07-21T14:46:56
LocalDateTime localDateTime2 = localDateTime.plus(1, ChronoUnit.YEARS);
System.out.println(""+localDateTime2);
//2022-07-21T14:46:56
//增加5天
LocalDateTime localDateTime3 = localDateTime.plusDays(5);
System.out.println(""+localDateTime3);
//2021-07-26T14:46:56
LocalDateTime localDateTime4 = localDateTime.plus(5, ChronoUnit.DAYS);
System.out.println(""+localDateTime4);
//2021-07-26T14:46:56
2、减少、年数、月数、天数等使用minus方法
LocalDateTime localDateTime = LocalDateTime.of(2021, Month.JULY, 21, 14, 46, 56);
System.out.println("设置日期时间:"+localDateTime);
//设置日期时间:2021-07-21T14:46:56
//减少2个月
LocalDateTime localDateTime1 = localDateTime.minusMonths(2);
System.out.println(String.valueOf(localDateTime1));
//结果: 2021-05-21T14:46:56
LocalDateTime localDateTime2 = localDateTime.minus(2, ChronoUnit.MONTHS);
System.out.println(String.valueOf(localDateTime2));
//结果: 2021-05-21T14:46:56
//减少2秒
LocalDateTime localDateTime3 = localDateTime.minusSeconds(2);
System.out.println(String.valueOf(localDateTime3));
//结果: 2021-07-21T14:46:54
LocalDateTime localDateTime4 = localDateTime.minus(2, ChronoUnit.SECONDS);
System.out.println(String.valueOf(localDateTime4));
//结果:2021-07-21T14:46:54
3、修改年月日时分秒通过with
LocalDateTime localDateTime = LocalDateTime.of(2021, Month.JULY, 21, 14, 46, 56);
System.out.println("设置日期时间:"+localDateTime);
//设置日期时间:2021-07-21T14:46:56
LocalDateTime localDateTime1 = localDateTime.withYear(2011);
System.out.println(localDateTime1);
//2011-07-21T14:46:56
LocalDateTime localDateTime2 = localDateTime.withHour(5);
System.out.println(localDateTime2);
//2021-07-21T05:46:56
4、格式化时间
LocalDateTime localDateTime = LocalDateTime.of(2021, Month.JULY, 21, 14, 46, 56);
System.out.println("设置日期时间:"+localDateTime);
//设置日期时间:2021-07-21T14:46:56
String time1 = localDateTime.format(DateTimeFormatter.BASIC_ISO_DATE);
System.out.println(time1);
//20210721
String time2 = localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE);
System.out.println(time2);
//2021-07-21
String time3 = localDateTime.format(DateTimeFormatter.ISO_LOCAL_TIME);
System.out.println(time3);
//14:46:56
String time4 = localDateTime.format(DateTimeFormatter.ISO_DATE_TIME);
System.out.println(time4);
//2021-07-21T14:46:56
//自定义格式化
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
String time8 = localDateTime.format(dateTimeFormatter);
System.out.println(time8);
//2021-07-21 02:46:56
5、时间比较使用 isAfter \ isBefore
LocalDateTime localDateTime = LocalDateTime.of(2021, Month.JULY, 21, 14, 46, 56);
System.out.println("设置日期时间:"+localDateTime);
//设置日期时间:2021-07-21T14:46:56
LocalDateTime localDateTime1 = LocalDateTime.of(2021, Month.JULY, 21, 15, 46, 56);
System.out.println("设置日期时间:"+localDateTime1);
//设置日期时间:2021-07-21T14:46:56
System.out.println(localDateTime.isAfter(localDateTime1));
System.out.println(localDateTime.isBefore(localDateTime1));
6、与Date的转换
LocalDateTime localDateTime = LocalDateTime.of(2021, Month.JULY, 21, 14, 46, 56);
System.out.println("设置日期时间:" + localDateTime);
//设置日期时间:2021-07-21T14:46:56
//转换为时间戳
ZoneId zoneId = ZoneId.systemDefault();
Instant instant = localDateTime.atZone(zoneId).toInstant();
System.out.println(instant.toEpochMilli());
//1626850016000
//时间戳转LocalDateTime
Instant instant1 = Instant.ofEpochMilli(1626850016000L);
ZoneId zone1 = ZoneId.systemDefault();
LocalDateTime localDateTime1 = LocalDateTime.ofInstant(instant1, zone1);
System.out.println(localDateTime1);
//2021-07-21T14:46:56
//Date转LocalDateTime
Date date = new Date();
Instant instant2 = date.toInstant();
ZoneId zoneId2 = ZoneId.systemDefault();
LocalDateTime localDateTime2 = instant2.atZone(zoneId2).toLocalDateTime();
System.out.println(localDateTime2);
// 2021-07-21T19:56:42.387
// LocalDateTime转Date
ZoneId zoneId3 = ZoneId.systemDefault();
ZonedDateTime zdt3 = localDateTime.atZone(zoneId3);
Date from3 = Date.from(zdt3.toInstant());
System.out.println(from3);
//Wed Jul 21 14:46:56 CST 2021