JDK 8中增加了一套全新的日期时间API,这套API设计合理,是线程安全的。新的日期及时间API位于 java.time 包中,下面是一些关键类:
LocalDate、LocalTime、LocalDateTime类的实例是不可变的对象!
LocalDate:获取日期时间的信息。格式为 2019-10-16
import java.time.LocalDate;
public class DD {
public static void main(String[] args) throws Exception {
// 创建指定日期
LocalDate fj = LocalDate.of(1985, 9, 23);
System.out.println("fj = " + fj);
// 得到当前日期
LocalDate nowDate = LocalDate.now();
System.out.println("nowDate = " + nowDate);
// 获取日期信息
System.out.println("年: " + nowDate.getYear());
System.out.println("月: " + nowDate.getMonthValue());
System.out.println("日: " + nowDate.getDayOfMonth());
System.out.println("星期: " + nowDate.getDayOfWeek());
}
}
//控制台打印:
fj = 1985-09-23
nowDate = 2020-07-23
年: 2020
月: 7
日: 23
星期: THURSDAY
LocalTime类: 获取时间信息。格式为 16:38:54.158549300
import java.time.LocalTime;
public class DD {
public static void main(String[] args) throws Exception {
// 得到指定的时间
LocalTime time = LocalTime.of(12,15, 28, 129_900_000);
System.out.println("time = " + time);
// 得到当前时间
LocalTime nowTime = LocalTime.now();
System.out.println("nowTime = " + nowTime);
// 获取时间信息
System.out.println("小时: " + nowTime.getHour());
System.out.println("分钟: " + nowTime.getMinute());
System.out.println("秒: " + nowTime.getSecond());
System.out.println("纳秒: " + nowTime.getNano());
}
}
//控制台打印:
time = 12:15:28.129900
nowTime = 15:13:40.778
小时: 15
分钟: 13
秒: 40
纳秒: 778000000
LocalDateTime类: 获取日期时间信息。格式为 2018-09-06T15:33:56.750
import java.time.LocalDateTime;
public class DD {
public static void main(String[] args) throws Exception {
LocalDateTime fj = LocalDateTime.of(1985, 9, 23, 9, 10, 20);
System.out.println("fj = " + fj);
// 得到当前日期时间
LocalDateTime now = LocalDateTime.now();
System.out.println("now = " + now);
System.out.println("年: " + now.getYear());
System.out.println("月: " + now.getMonthValue());
System.out.println("日: " + now.getDayOfMonth());
System.out.println("小时: " + now.getHour());
System.out.println("分钟: " + now.getMinute());
System.out.println("秒: " + now.getSecond());
System.out.println("纳秒: " + now.getNano());
}
}
//控制台打印:
fj = 1985-09-23T09:10:20
now = 2020-07-23T15:17:59.243
年: 2020
月: 7
日: 23
小时: 15
分钟: 17
秒: 59
纳秒: 243000000
对日期时间的修改,对已存在的LocalDate对象,创建它的修改版,最简单的方式是使用withAttribute方法。withAttribute方法会创建对象的一个副本,并按照需要修改它的属性。以下所有的方法都返回了一个修改属性的对象,他们不会影响原来的对象
import java.time.LocalDateTime;
public class DD {
public static void main(String[] args) throws Exception {
LocalDateTime now = LocalDateTime.now();
System.out.println("now = " + now);
// 修改日期时间
LocalDateTime setYear = now.withYear(2078);
System.out.println("修改年份: " + setYear);
System.out.println("now == setYear: " + (now == setYear));
System.out.println("修改月份: " + now.withMonth(6));
System.out.println("修改小时: " + now.withHour(9));
System.out.println("修改分钟: " + now.withMinute(11));
// 再当前对象的基础上加上或减去指定的时间
LocalDateTime localDateTime = now.plusDays(5);
System.out.println("5天后: " + localDateTime);
System.out.println("10年后: " + now.plusYears(10));
System.out.println("20月后: " + now.plusMonths(20));
System.out.println("20年前: " + now.minusYears(20));
System.out.println("5月前: " + now.minusMonths(5));
System.out.println("100天前: " + now.minusDays(100));
}
}
//控制台打印:
now = 2020-07-23T15:23:55.436
修改年份: 2078-07-23T15:23:55.436
now == setYear: false
修改月份: 2020-06-23T15:23:55.436
修改小时: 2020-07-23T09:23:55.436
修改分钟: 2020-07-23T15:11:55.436
5天后: 2020-07-28T15:23:55.436
10年后: 2030-07-23T15:23:55.436
20月后: 2022-03-23T15:23:55.436
20年前: 2000-07-23T15:23:55.436
5月前: 2020-02-23T15:23:55.436
100天前: 2020-04-14T15:23:55.436
在JDK8中,使用isBefore()、isAfter()、equals()方法来比较两个日期,可直接进行比较!
import java.time.LocalDate;
public class DD {
public static void main(String[] args) throws Exception {
LocalDate now = LocalDate.now();
LocalDate date = LocalDate.of(2018, 8, 8);
System.out.println(now.isBefore(date));
System.out.println(now.isAfter(date));
LocalDateTime now = LocalDateTime.now();
LocalDateTime now2 = LocalDateTime.of(1985, 9, 23, 9, 10, 20);
System.out.println(now.isAfter(now2));
System.out.println(now.isBefore(now2));
System.out.println(now.equals(now2));
}
}
//控制台打印:
false
true
true
false
false
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class DD {
public static void main(String[] args) throws Exception {
// 得到当前日期时间
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// 将日期时间格式化为字符串
String format = now.format(formatter);
System.out.println("format = " + format);
// 将字符串解析为日期时间
LocalDateTime parse = LocalDateTime.parse("1985-09-23 10:12:22", formatter);
System.out.println("parse = " + parse);
}
}
//控制台打印:
format = 2020-07-23 15:28:24
parse = 1985-09-23T10:12:22
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Period;
public class DD {
public static void main(String[] args) throws Exception {
Instant now1 = Instant.now();
System.out.println("当前时间戳 = " + now1);
// Duration计算时间的距离
LocalTime now = LocalTime.now();
LocalTime time = LocalTime.of(14, 15, 20);
Duration duration = Duration.between(time, now);
System.out.println("相差的天数:" + duration.toDays());
System.out.println("相差的小时数:" + duration.toHours());
System.out.println("相差的分钟数:" + duration.toMinutes());
// Period计算日期的距离
LocalDate nowDate = LocalDate.now();
LocalDate date = LocalDate.of(1998, 8, 8);
Period period = Period.between(date, nowDate);
System.out.println("相差的年:" + period.getYears());
System.out.println("相差的月:" + period.getMonths());
System.out.println("相差的天:" + period.getDays());
}
}
//控制台打印:
当前时间戳 = 2020-07-23T07:32:20.326Z
相差的天数:0
相差的小时数:1
相差的分钟数:77
相差的年:21
相差的月:11
相差的天:15
Java8 中加入了对时区的支持,LocalDate、LocalTime、LocalDateTime是不带时区的,带时区的日期时间类分别为:ZonedDate、ZonedTime、ZonedDateTime。
其中每个时区都对应着 ID,ID的格式为 “区域/城市” 。例如 :Asia/Shanghai 等。
ZoneId:该类中包含了所有的时区信息
import java.time.Clock;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class DD {
public static void main(String[] args) throws Exception {
// ZoneId.getAvailableZoneIds().forEach(System.out::println)可获取所有的时区ID;
// 获取计算机的当前日期时间
LocalDateTime now = LocalDateTime.now();
System.out.println("now = " + now);
// now(Clock.systemUTC()): 创建世界标准时间
ZonedDateTime bz = ZonedDateTime.now(Clock.systemUTC());
System.out.println("bz = " + bz);
// now(): 使用计算机的默认的时区(东八区),创建日期时间
ZonedDateTime now1 = ZonedDateTime.now();
System.out.println("now1 = " + now1);
// 使用指定的时区创建日期时间
ZonedDateTime now2 = ZonedDateTime.now(ZoneId.of("America/Vancouver"));
System.out.println("now2 = " + now2);
}
}
//控制台打印:
now = 2020-07-23T15:39:16.286
bz = 2020-07-23T07:39:16.287Z
now1 = 2020-07-23T15:39:16.287+08:00[Asia/Shanghai]
now2 = 2020-07-23T00:39:16.288-07:00[America/Vancouver]
学习了Instant类,方便操作秒和纳秒,一般是给程序使用的。学习Duration/Period计算日期或时间的距离,还使用时间调整器方便的调整时间,学习了带时区的3个类ZoneDate/ZoneTime/ZoneDateTime
JDK 8新的日期和时间 API的优势:
以上为学习所作笔记,来源为黑马程序员资料JDK8新特性.pdf