Java 8优雅处理时间和日期

目录

一 基本操作

1.1 生成日期

1.2 生成时间

1.3 生成年

1.4 生成月

1.5 生成日

1.6 生成时

1.7 生成分

1.8 生成秒

1.9 生成日期和时间

二 日常应用

2.1  Instant 与 Date 互转

2.2 LocalDateTime 与 Date 互转

2.3 日期时间格式化输出

2.4 两个日期或者时间进行比较


用过 java.util.Date 和 java.sql.Date 的或多或少都会觉得这这两个日期 API 包设计的太不友好,太反人类。比如:

  • 什么 java.util.Date 是可变类型;
  • SimpleDateFormatter 不是线程安全的;
  • 甚至 java.util.Date 月份从0开始,一月是0,十二月是11等等。

为了解决这些痛点,Java 8 的 java.time 包出现了,该包下的所有类都是不可变类型,而且都是线程安全的。

你可能会发现该包的很多 API 与 JodaTime 类似,因为 Java 8 的时间包设计参考了 JodaTime 设计。

Java 8中,java.time 包如下所示:

Clock:时钟,比如获取当前中国北京的时间。
Instant:时间戳。
Duration:持续时间,用于计算时间差。
LocalDate:只包含日期,比如:2021-01-17 。
LocalTime:只包含时间,比如:14:15:10。
LocalDateTime:同时包含日期和时间,比如:2021-01-170 14:15:10。
Period:时间段,用于计算时间差。
ZoneOffset:时区偏移量,比如:+8:00。
ZonedDateTime:带时区的时间。

其他都是一些很简单的类,都是比较见名知意的。

一 基本操作

1.1 生成日期

//当前日期
LocalDate localDate =  LocalDate.now();

//指定日期
LocalDate localDate = LocalDate.of(2021, 01, 17);

1.2 生成时间

//当前时间
LocalTime localTime1 =  LocalTime.now();
//指定时间
LocalTime localTime2= LocalTime.of(15, 11, 15);

1.3 生成年

//今年
int now = LocalDate.now().getYear()

//明年
int after = LocalDate.now().plusYears(1).getYear();

//去年
int before = LocalDate.now().minusYears(1).getYear();

1.4 生成月

//这个月
int now = LocalDate.now().getMonthValue()

//下个月
int after = LocalDate.now().plusMonths(1).getMonthValue();

//上个月
int before = LocalDate.now().minusMonths(1).getMonthValue();

1.5 生成日

//今天
int now = LocalDate.now().getDayOfMonth();
//明天
int after = LocalDate.now().plusDays(1).getDayOfMonth();
//昨天
int before = LocalDate.now().minusDays(1).getDayOfMonth();

1.6 生成时

//这个时辰
int now = LocalTime.now().getHour();
//下个时辰
int after = LocalTime.now().plusHours(1).getHour();
//上个时辰
int before = LocalTime.now().minusHours(1).getHour();

1.7 生成分

//这一分钟
int now = LocalTime.now().getMinute();
//下一分钟
int after = LocalTime.now().plusMinutes(1).getMinute();
//上一分钟
int before = LocalTime.now().minusMinutes(1).getMinute();

1.8 生成秒

//这一秒
int now = LocalTime.now().getSecond();
//下一秒
int after = LocalTime.now().plusSeconds(1).getSecond();
//上一秒
int before = LocalTime.now().minusSeconds(1).getSecond();

1.9 生成日期和时间

LocalDateTime localDateTime = LocalDateTime.now();

二 日常应用

2.1  Instant 与 Date 互转

2.1.1 Instant 转 Date

Instant instant  = Instant.now();
Date date = Date.from(instant);

2.1.2 Date 转 Instant

Date date = new Date();
Instant instant = date.toInstant();

2.2 LocalDateTime 与 Date 互转

2.2.1 LocalDateTime 转 Date

LocalDateTime localDateTime = LocalDateTime.now();
Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
Date date = Date.from(instant);

2.2.2 Date 转 LocalDateTime

Date date = new Date();
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());

2.3 日期时间格式化输出

2.3.1 LocalDateTime 格式化输出

LocalDateTime localDateTime = LocalDateTime.now();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String string = dateTimeFormatter.format(localDateTime);

2.3.2 LocalDate 格式化输出

LocalDate data=LocalDate.now();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy:MM:dd");
String string = dateTimeFormatter.format(data);

2.4 两个日期或者时间进行比较

2.4.1 日期前后对比

LocalDate today = LocalDate.now();
LocalDate speDay = LocalDate.of(2021,01,18);
boolean isAfter = today.isAfter(speDay);

2.4.2 时间前后对比

LocalTime today = LocalTime.now();
LocalTime speDay = LocalTime.of(15,01,18);
boolean isAfter = today.isAfter(speDay);

2.4.3  LocalDateTime 比较

LocalDateTime begin = LocalDateTime.of(2021,01, 17, 15, 13, 14);
LocalDateTime end = LocalDateTime.of(2021,01, 18, 15, 13, 14);
Duration duration = Duration.between(begin, end);
long days = duration.toDays();
long hours = duration.toHours();
long minutes = duration.toMinutes();
//。。。。。。

2.4.4 LocalDate 比较

LocalDate begin = LocalDate.of(2021,01, 17);
LocalDate end = LocalDate.of(2021,01, 18);
Period period = Period.between(begin, end);
int months = period.getMonths();
int days = period.getDays();

猜你喜欢

转载自blog.csdn.net/jack1liu/article/details/112742282