Java 8中的时间处理
在Java8中, 新的时间及⽇期API位于java.time包中, 该包中有哪些重要的类。 分别代表了什么?
Instant: 时间戳
Duration: 持续时间, 时间差
LocalDate: 只包含⽇期, ⽐如: 2016-10-20
LocalTime: 只包含时间, ⽐如: 22:15:16
LocalDateTime: 包含⽇期和时间, ⽐如: 2016-10-20 22:15:16
Period: 时间段
ZoneOffset: 时区偏移量, ⽐如: +8:00
ZonedDateTime: 带时区的时间
Clock: 时钟, ⽐如获取⽬前美国纽约的时间
LocalTime 和 LocalDate的区别?
LocalDate表⽰⽇期, 年⽉⽇, LocalTime表⽰时间, 时分 秒
获取当前时间
在Java8中,使用如下方式获取当前时间:
LocalDate today =LocalDate.now();int year =today.getYear();int month =today.getMonthValue();int day =today.getDayOfMonth();
System.out.printf("Year : %d Month : %d day : %d t %n", year,month, day);
创建指定日期的时间
LocalDate date = LocalDate.of(2018, 01, 01);
LocalDateTime create_localDateTime = LocalDateTime.ofEpochSecond(1593488632L, 0, ZoneOffset.ofHours(8));
检查闰年
直接使⽤LocalDate的isLeapYear即可判断是否闰年
LocalDate nowDate =LocalDate.now();//判断闰年
boolean leapYear = nowDate.isLeapYear();
时间相隔
Duraction表示:时间的区间,用来度量秒和纳秒之间的时间值
Period表示:一段时间的区间,用来度量年月日和几天之间的时间值
Period period = Period.between(LocalDate.of(2018, 1, 1), LocalDate.of(2019, 2, 2));
System.out.println("Years:" + period.getYears());//1
System.out.println("Months:" + period.getMonths());//1
System.out.println("Days:" + period.getDays());//1
System.out.println("TotalMonths:" + period.toTotalMonths());//13
LocalDateTime ldt1 = LocalDateTime.of(2018, 1, 1, 1, 1);
LocalDateTime ldt2 = LocalDateTime.of(2019, 2, 2, 2, 2);
Duration duration = Duration.between(ldt1, ldt2);
System.out.println("Days:" + duration.toDays());//397
System.out.println("Hours:" + duration.toHours());//9529
System.out.println("Minutes:" + duration.toMinutes());//571741
时间比较
System.out.println("ldt1是否在ldt2之前:" + ldt1.isBefore(ldt2));
时间加减
LocalDate now = LocalDate.now();
LocalDate now_plusDays_1 = now.plusDays(1);
LocalDate now_plus_Days_1 = now.plus(1, ChronoUnit.DAYS);
//before 5 hours and 30 minutes
LocalDateTime dateTime = LocalDateTime.now().minusHours(5).minusMinutes(30);
利用DateTimeFormatter 进行LocalDateTime与String互转
{//LocalDateTime转String(LocalDate,LocalTime类似)
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime localDateTime=LocalDateTime.now();
String localDateTimeStr=dtf.format(localDateTime);
System.out.println("LocalDateTime转成String类型的时间:" + localDateTimeStr);//2020-06-01 13:23:32
System.out.println("LocalDateTime转成String类型的时间:" + localDateTime.toString());//2020-06-01T13:23:32.430
}
{//String转LocalDateTime(LocalDate,LocalTime类似)
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime localDateTime= LocalDateTime.parse("2018-06-01 10:12:05", dtf);
System.out.println("String类型的时间转成LocalDateTime:" + localDateTime);//2018-06-01T10:12:05
}
利用Instant 进行LocalDateTime与Date互转
{//LocalDateTime转Date
LocalDateTime localDateTime =LocalDateTime.now();
ZoneId zone=ZoneId.systemDefault();
Instant instant=localDateTime.atZone(zone).toInstant();
Date nowDate=Date.from(instant);
System.out.println(nowDate);//Mon Jun 01 13:55:40 CST 2020
}
{//Date转LocalDateTime
Date nowDate = newDate();
Instant instant=nowDate.toInstant();
ZoneId zone=ZoneId.systemDefault();
LocalDateTime localDateTime=LocalDateTime.ofInstant(instant, zone);
System.out.println(localDateTime);//2020-06-01T13:59:21.315
}
{//LocalDate转Date
LocalDate localDate =LocalDate.now();
ZoneId zone=ZoneId.systemDefault();
Instant instant=localDate.atStartOfDay().atZone(zone).toInstant();
Date nowDate=Date.from(instant);
System.out.println(nowDate);//Mon Jun 01 00:00:00 CST 2020
}
{//Date转LocalDate
Date date = newDate();
Instant instant=date.toInstant();
ZoneId zone=ZoneId.systemDefault();
LocalDateTime localDateTime=LocalDateTime.ofInstant(instant, zone);
LocalDate localDate=localDateTime.toLocalDate();
System.out.println(localDate);//2020-06-01
}
{//LocalTime转Date
LocalTime localTime =LocalTime.now();
LocalDate localDate=LocalDate.now();
LocalDateTime localDateTime=LocalDateTime.of(localDate, localTime);
ZoneId zone=ZoneId.systemDefault();
Instant instant=localDateTime.atZone(zone).toInstant();
Date nowDate=Date.from(instant);
System.out.println(nowDate);//Mon Jun 01 14:24:29 CST 2020
}
{//Date转LocalTime
Date date = newDate();
Instant instant=date.toInstant();
ZoneId zone=ZoneId.systemDefault();
LocalDateTime localDateTime=LocalDateTime.ofInstant(instant, zone);
LocalTime localTime=localDateTime.toLocalTime();
System.out.println(localTime);//14:24:29.686
}
LocalDateTime,LocalDate,LocalTime互转
{//LocalDateTime转LocalDate
LocalDateTime localDateTime =LocalDateTime.now();
LocalDate localDate=localDateTime.toLocalDate();
System.out.println(localDate);//2020-06-01
}
{//LocalDate转LocalDateTime
LocalDate localDate =LocalDate.now();
ZoneId zone=ZoneId.systemDefault();
Instant instant=localDate.atStartOfDay().atZone(zone).toInstant();
Date date=Date.from(instant);
System.out.println(date);//Mon Jun 01 00:00:00 CST 2020
}
{//LocalDateTime转LocalTime
LocalDateTime localDateTime =LocalDateTime.now();
LocalTime localTime=localDateTime.toLocalTime();
System.out.println(localTime);//14:29:44.243
}
{//LocalTime转LocalDateTime
LocalTime localTime =LocalTime.now();
LocalDate localDate=LocalDate.now();
LocalDateTime localDateTime=LocalDateTime.of(localDate, localTime);
System.out.println(localDateTime);//2020-06-01T14:31:35.366
}
其它
有的时候,你需要进行一些更加复杂的操作,比如,将日期调整到下个周日、下个工作日,或者是本月的最后一天。这时,你可以使用重载版本的with方法,向其传递一个提供了更多定制化选择的TemporalAdjuster对象,更 加 灵 活 地 处 理 日 期。
public static voidmain(String[] args) {
LocalDate date= LocalDate.parse("2020-07-07");//获取这个月的第一个周日的时间
System.out.println(date.with(TemporalAdjusters.dayOfWeekInMonth(1, DayOfWeek.SUNDAY)));//输出: 2020-07-05//获取上个月的最后一周日的时间
System.out.println(date.with(TemporalAdjusters.dayOfWeekInMonth(0, DayOfWeek.SUNDAY)));//输出: 2020-06-28//获取这个月的倒数第一个周日的时间
System.out.println(date.with(TemporalAdjusters.dayOfWeekInMonth(-1, DayOfWeek.SUNDAY)));//输出: 2020-07-26//获取这个月的第一个周日的时间,上面的dayOfWeekInMonth更灵活,可以定义第几周
System.out.println(date.with(TemporalAdjusters.firstInMonth(DayOfWeek.SUNDAY)));//输出: 2020-07-05//明年的第一天
System.out.println(date.with(TemporalAdjusters.firstDayOfNextYear()));//输出: 2021-01-01//获取下个周五的时间
System.out.println(date.with(TemporalAdjusters.next(DayOfWeek.FRIDAY)));//输出: 2020-07-10//获取本月最后一天
System.out.println(date.with(TemporalAdjusters.lastDayOfMonth()));//输出: 2020-07-31//获取本月第一天
System.out.println(date.with(TemporalAdjusters.firstDayOfMonth()));//输出: 2020-07-01
}
public static voidmain(String[] args) {
DateTimeFormatter dateTimeFormatter= DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");//一天开始时间
LocalDateTime todayStart =LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
String format=todayStart.format(dateTimeFormatter);
System.out.println("format = " +format);//输出: format = 2020-07-07 00:00:00//一天结束时间
LocalDateTime todayEnd =LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
String format1=todayEnd.format(dateTimeFormatter);
System.out.println("format1 = " +format1);//输出: format1 = 2020-07-07 23:59:59//一天中午时间
LocalDateTime todayMid =LocalDateTime.of(LocalDate.now(), LocalTime.NOON);
String format2=todayMid.format(dateTimeFormatter);
System.out.println("format2 = " +format2);//输出: format2 = 2020-07-07 12:00:00
}