java8 新特性—java.time包 时间处理类
Instant 时间戳
Duration 持续时间、时间差
LocalDate 只包含日期,比如:2018-09-24
LocalTime 只包含时间,比如:10:32:10
LocalDateTime 包含日期和时间,比如:2018-09-24 10:32:10
Peroid 时间段
ZoneOffset 时区偏移量,比如:+8:00
ZonedDateTime 带时区的日期时间
Clock 时钟,可用于获取当前时间戳
java.time.format.DateTimeFormatter 时间格式化类
用例
LocalDate类
构造
LocalDate date = LocalDate.now();
LocalDate date = LocalDate.of(2019, 12, 11);
......
// 本地日期
LocalDate localDate = LocalDate.of(2019, 12, 11);
System.out.println(localDate);
int year = localDate.getYear(); //哪一年
System.out.println("year = " + year);
Month month = localDate.getMonth(); // 第几个月
System.out.println("month = " + month);
int dayOfMonth = localDate.getDayOfMonth(); //一个月的第几天
System.out.println("dayOfMonth = " + dayOfMonth);
DayOfWeek dayOfWeek = localDate.getDayOfWeek(); //一周的第几天
System.out.println("dayOfWeek = " + dayOfWeek);
int lenth = localDate.lengthOfMonth(); // 一个月的天数
System.out.println("lenth = " + lenth);
boolean leapYear = localDate.isLeapYear(); // 是否是闰年
System.out.println("leapYear = " + leapYear);
// 时间加减操作
LocalDate date = LocalDate.now();
LocalDate localDate2 = date.minusDays(5);
LocalDate localDate3 = date.plusYears(10);
date.plus(5, ChronoUnit.DAYS);
......
获取年月日信息
LocalDate date = LocalDate.now();
System.out.printf("年=%d, 月=%d, 日=%d", date.getYear(), date.getMonthValue(), date.getDayOfMonth());
......
比较两个日期是否相等
LocalDate now = LocalDate.now();
LocalDate date = LocalDate.of(2019, 12, 11);
System.out.println("日期是否相等=" + now.equals(date));
......
LocalTime类
获取当前时间时分秒
LocalTime time = LocalTime.now();
......
// 解析日期
String dateText = "20180924";
LocalDate date = LocalDate.parse(dateText, DateTimeFormatter.BASIC_ISO_DATE);
System.out.println("格式化之后的日期=" + date);
// 格式化日期
dateText = date.format(DateTimeFormatter.ISO_DATE);
System.out.println("dateText=" + dateText);
......
比较与计算
日期时间计算
// 时间增量
LocalTime time = LocalTime.now();
LocalTime newTime = time.plusHours(2);
System.out.println("newTime=" + newTime);
......
// 日期增量
LocalDate date = LocalDate.now();
LocalDate newDate = date.plus(1, ChronoUnit.WEEKS);
System.out.println("newDate=" + newDate);
......
日期时间比较
LocalDate now = LocalDate.now();
LocalDate date1 = LocalDate.of(2019, 12, 11);
if (now.isAfter(date1)) {
System.out.println("2019年已经过去了");
}
LocalDate date2 = LocalDate.of(2020, 1, 1);
if (now.isBefore(date2)) {
System.out.println("2020年还未到来");
}
......
LocalDateTime类
//LocalDateTime包括LocalDate和LocalTime
LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);
System.out.println("localDateTime = " + localDateTime);
//LocalDateTime和LocalDate, LocalTime相互转换
LocalDate localDate1 = localDateTime.toLocalDate();
LocalTime localTime1 = localDateTime.toLocalTime();
......
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// 日期时间转字符串
LocalDateTime now = LocalDateTime.now();
String nowText = now.format(formatter);
System.out.println("nowText=" + nowText);
// 字符串转日期时间
String datetimeText = "2019-12-11 23:59:59";
LocalDateTime datetime = LocalDateTime.parse(datetimeText, formatter);
System.out.println(datetime);
......
Instant类
// 一个时间戳
Instant instant = Instant.now();
......
Duration类
// 一个时间段
Duration duration = Duration.between(localDateTime, localTime1);
long toDays = duration.toDays(); // 这个时间段中有几天
long toHours = duration.toHours(); // 这个时间段中有几个小时
// 通过of创建时间段
Duration duration1 = Duration.of(7, ChronoUnit.DAYS);
......
Period类
// 以年月日来表示时间段
Period period = Period.between(localDate, localDate1);
......
// 上海时间
ZoneId shanghaiZoneId = ZoneId.of("Asia/Shanghai");
ZonedDateTime shanghaiZonedDateTime = ZonedDateTime.now(shanghaiZoneId);
// 东京时间
ZoneId tokyoZoneId = ZoneId.of("Asia/Tokyo");
ZonedDateTime tokyoZonedDateTime = ZonedDateTime.now(tokyoZoneId);
......