JDK 8中增加了一套全新的日期时间API,这套API设计合理,是线程安全的。新的日期及时间API位于java.time包中,下面是一些关键类。
public static void main(String[] args) {
//创建指定的日期
LocalDate date = LocalDate.of(2022, 2, 22);
System.out.println("data1 = " + date);
//得到当前的日期
LocalDate now = LocalDate.now();
System.out.println("date2 = " + now);
//根据LocalData对象获取对应的日期信息
System.out.println("年:"+ now.getYear());
System.out.println("月:"+ now.getMonth());
System.out.println("月:"+ now.getMonthValue());
System.out.println("月:"+ now.getMonth().getValue());
System.out.println("日:"+ now.getDayOfMonth());
System.out.println("星期:"+ now.getDayOfWeek());
System.out.println("星期:"+ now.getDayOfWeek().getValue());
}
输出:
date = 2022-02-22
now = 2022-07-26
年:2022
月:JULY
月:7
月:7
日:26
星期:TUESDAY
星期:2
public static void main(String[] args) {
//得到指定的时间
LocalTime time = LocalTime.of(14,22,31,22222);
System.out.println("time:"+time);
//获取当前的时间
LocalTime now = LocalTime.now();
System.out.println("now:"+now);
//获取时间信息
System.out.println(now.getHour());
System.out.println(now.getMinute());
System.out.println(now.getSecond());
System.out.println(now.getNano());
}
输出:
time:14:22:31.000022222
now:14:26:40.822
14
26
40
822000000
public static void main(String[] args) {
//得到指定的日期时间
LocalDateTime localDateTime =
LocalDateTime.of(2022,
2,
22,
12,
12,
12,
2222);
System.out.println("localDateTime:"+localDateTime);
//获取当前的日期时间
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.getDayOfWeek().getValue());
System.out.println(now.getHour());
System.out.println(now.getMinute());
System.out.println(now.getSecond());
System.out.println(now.getNano());
}
输出:
localDateTime:2022-02-22T12:12:12.000002222
now:2022-07-26T14:34:56.084
2022
7
26
2
14
34
56
84000000
now.with();//修改
now.plus();//加上
now.minus();//减去
//比较
now.isAfter();//是否在后
now.isBefore();//是否在前
now.isEqual();//相等
注意:在进行日期时间修改的时候,原来的LocalDate是不会被修改的,每次操作都是返回了一个新的LocalDate对象,所以在多线程场景下是数据安全的。
在JDK8中我们可以通过java.time.format.DateTimeFormatter
类进行日期的解析和格式化操作。
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
//指定格式 使用系统默认的格式
DateTimeFormatter isoLocalDateTime = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
//将日期时间转换为字符串
String format = now.format(isoLocalDateTime);
System.out.println("format = " + format);//2022-07-26T15:01:35.752
//通过ofPattern 方法来指定特定的格式
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format1 = now.format(dateTimeFormatter);
System.out.println("format1 = " + format1);//2022-07-26 15:01:35
//将字符串解析为一个 日期时间类型
LocalDateTime parse = LocalDateTime.parse("1999-06-27 22:55:16", dateTimeFormatter);
System.out.println("parse = " + parse);//1999-06-27T22:55:16
}
输出:
format = 2022-07-26T15:01:35.752
format1 = 2022-07-26 15:01:35
parse = 1999-06-27T22:55:16
public static void main(String[] args) {
//获取所有的时区id
// ZoneId.getAvailableZoneIds().forEach(System.out::println);
//获取当前时间 中国使用的 东八区的时区,比标准时间早8个小时
LocalDateTime now = LocalDateTime.now();
System.out.println("now = " + now);//2022-07-26T15:56:27.623
//获取标准时间
ZonedDateTime bz = ZonedDateTime.now(Clock.systemUTC());
System.out.println("bz = " + bz);//2022-07-26T07:56:27.624Z
//使用计算机默认的时区,创建日期时间
ZonedDateTime now1 = ZonedDateTime.now();
System.out.println("now1 = " + now1);//2022-07-26T15:56:27.625+08:00[Asia/Shanghai]
//使用指定的时区创建日期时间
ZonedDateTime now2 = ZonedDateTime.now(ZoneId.of("America/Marigot"));
System.out.println("now2 = " + now2);//2022-07-26T03:59:04.910-04:00[America/Marigot]
}
输出:
now = 2022-07-26T15:59:04.895
bz = 2022-07-26T07:59:04.909Z
now1 = 2022-07-26T15:59:04.909+08:00[Asia/Shanghai]
now2 = 2022-07-26T03:59:04.910-04:00[America/Marigot]