Java 8常用时间 API

Date: 你不爱我了吗? 🚡


在Java 8中,Instant类用于表示时间戳,相当于旧的Date类;LocalDateTime类用于表示日期和时间,相当于旧的Calendar类;DateTimeFormatter类用于格式化日期和时间,相当于旧的SimpleDateFormat类

本地时间

  1. LocalDate:表示日期,如:2023-04-06。
LocalDate now = LocalDate.now(); // 获取当前日期
LocalDate date = LocalDate.of(2023, 4, 6); // 通过年、月、日创建LocalDate对象
LocalDate plusDate = date.plusDays(1); // 增加1天
LocalDate minusDate = date.minusDays(1); // 减少1天
int year = date.getYear(); // 获取年份
int month = date.getMonthValue(); // 获取月份
int day = date.getDayOfMonth(); // 获取日期

  1. LocalTime:表示时间,如:14:30:00。
LocalTime now = LocalTime.now(); // 获取当前时间
LocalTime time = LocalTime.of(14, 30); // 通过小时和分钟创建LocalTime对象
LocalTime plusTime = time.plusHours(1); // 增加1小时
LocalTime minusTime = time.minusMinutes(30); // 减少30分钟
int hour = time.getHour(); // 获取小时
int minute = time.getMinute(); // 获取分钟

  1. LocalDateTime:表示日期时间,如:2023-04-06T14:30:00。
LocalDateTime now = LocalDateTime.now(); // 获取当前日期时间
LocalDateTime dateTime = LocalDateTime.of(2023, 4, 6, 14, 30); // 通过年、月、日、小时和分钟创建LocalDateTime对象
LocalDateTime plusDateTime = dateTime.plusDays(1); // 增加1天
LocalDateTime minusDateTime = dateTime.minusHours(2); // 减少2小时
int year = dateTime.getYear(); // 获取年份
int month = dateTime.getMonthValue(); // 获取月份
int day = dateTime.getDayOfMonth(); // 获取日期
int hour = dateTime.getHour(); // 获取小时
int minute = dateTime.getMinute(); // 获取分钟

  1. Instant:表示时间戳,即从1970年1月1日00:00:00开始的毫秒数。
Instant now = Instant.now(); // 获取当前时间戳
Instant instant = Instant.ofEpochMilli(1000); // 通过时间戳创建Instant对象
Instant plusInstant = instant.plusSeconds(10); // 增加10秒
Instant minusInstant = instant.minusMillis(500); // 减少500毫秒
long epochMilli = instant.toEpochMilli(); // 获取从1970年1月1日00:00:00开始的毫秒数

  1. Duration:表示时间间隔,如:2小时30分钟。
LocalDateTime start = LocalDateTime.of(2023, 4, 6, 14, 30);
LocalDateTime end = LocalDateTime.of(2023, 4, 6, 16, 0);
Duration duration = Duration.between(start, end); // 计算两个时间点之间的时间间隔
Duration plusDuration = duration.plusHours(1); // 增加1小时
Duration minusDuration = duration.minusMinutes(30); // 减少30分钟
long hours = duration.toHours(); // 获取总小时数
long minutes = duration.toMinutes(); // 获取总分钟数

  1. Period:表示日期间隔,如:2天。
LocalDate start = LocalDate.of(2023, 4, 6);
LocalDate end = LocalDate.of(2023, 4, 8);
Period period = Period.between(start, end); // 计算两个日期之间的日期间隔
Period plusPeriod = period.plusDays(1); // 增加1天
Period minusPeriod = period.minusWeeks(1); // 减少1周
int days = period.getDays(); // 获取天数
int months = period.getMonths(); // 获取月数

这些类都是线程安全的,不可变的,因此适合在多线程环境下使用。

在Java中,这些时间类的作用非常广泛,它们可以帮助我们处理日期时间相关的逻辑,例如计算时间差、格式化日期时间、比较时间、转换时区等等。在实际开发中,我们常常需要进行时间计算,例如计算两个时间点之间的时间差、计算某个日期是周几等等。这些时间类可以大大简化这些操作,使得我们的代码更加简洁、清晰、易于维护。

时区相关

  1. ZoneId:时区标识符,表示不同的时区,例如“Asia/Shanghai”。
ZoneId systemZone = ZoneId.systemDefault(); // 获取系统默认时区
ZoneId beijingZone = ZoneId.of("Asia/Shanghai"); // 根据时区ID创建ZoneId对象

  1. ZonedDateTime:带时区的日期时间,可以表示不同时区下的日期时间,例如“2023-04-06T14:30:00+08:00[Asia/Shanghai]”。
ZonedDateTime now = ZonedDateTime.now(); // 获取当前日期时间和时区信息的ZonedDateTime对象
ZonedDateTime zonedDateTime = ZonedDateTime.of(LocalDateTime.of(2023, 4, 6, 14, 30), ZoneId.of("Asia/Shanghai")); // 根据本地日期时间和时区创建ZonedDateTime对象
ZonedDateTime withZone = zonedDateTime.withZoneSameInstant(ZoneId.of("America/New_York")); // 修改时区,并且保持日期时间不变
int year = zonedDateTime.getYear(); // 获取年份
int month = zonedDateTime.getMonthValue(); // 获取月份
int day = zonedDateTime.getDayOfMonth(); // 获取日期
int hour = zonedDateTime.getHour(); // 获取小时
int minute = zonedDateTime.getMinute(); // 获取分钟
ZoneId zone = zonedDateTime.getZone(); // 获取时区信息

  1. OffsetDateTime:带时区偏移量的日期时间,可以表示相对于UTC的偏移量,例如“2023-04-06T14:30:00+08:00”。
OffsetDateTime now = OffsetDateTime.now(); // 获取当前日期时间和时区偏移量信息的OffsetDateTime对象
OffsetDateTime offsetDateTime = OffsetDateTime.of(LocalDateTime.of(2023, 4, 6, 14, 30), ZoneOffset.of("+08:00")); // 根据本地日期时间和时区偏移量创建OffsetDateTime对象
OffsetDateTime plusOffsetDateTime = offsetDateTime.plusHours(1); // 增加1小时
OffsetDateTime minusOffsetDateTime = offsetDateTime.minusMinutes(30); // 减少30分钟
int year = offsetDateTime.getYear(); // 获取年份
int month = offsetDateTime.getMonthValue(); // 获取月份
int day = offsetDateTime.getDayOfMonth(); // 获取日期
int hour = offsetDateTime.getHour(); // 获取小时
int minute = offsetDateTime.getMinute(); // 获取分钟
ZoneOffset offset = offsetDateTime.getOffset(); // 获取时区偏移量信息

  1. ZoneOffset:时区偏移量,表示相对于UTC的偏移量,例如“+08:00”。
ZoneOffset offset1 = Zone

这些与时区相关的时间类可以帮助我们处理跨时区的时间计算和表示。在处理全球化应用时,时区相关的操作尤其重要,因为不同地区的时间可能有巨大的差异。这些时间类提供了强大的功能来处理跨时区的时间计算和表示,使得我们可以更好地应对全球化应用的时间需求。

格式化

在JDK8中,如果要对时间进行格式化,可以使用以下几个类:

  1. DateTimeFormatter:日期时间格式化类,它提供了很多方法来将日期时间格式化为字符串或解析字符串为日期时间。

  2. SimpleDateFormat:日期时间格式化类,它是JDK早期版本中的日期时间格式化类,在JDK8中仍然保留。它使用一些预定义的格式化字符串来格式化日期时间。

这两个类都提供了格式化日期时间的功能,但是使用方式略有不同。DateTimeFormatter提供了更加灵活的API来定制日期时间格式,它支持解析和格式化ISO 8601格式的日期时间字符串,并且可以支持多种语言环境。SimpleDateFormat则更适合于一些简单的日期时间格式化场景,它使用的是预定义的格式化字符串,虽然使用起来比较简单,但是可定制性较差。

下面是一个使用DateTimeFormatter对日期时间进行格式化的示例代码:

LocalDateTime dateTime = LocalDateTime.of(2023, 4, 6, 14, 30);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDateTime = dateTime.format(formatter); // 将日期时间格式化为指定格式的字符串
LocalDateTime parsedDateTime = LocalDateTime.parse("2023-04-06 14:30:00", formatter); // 将指定格式的字符串解析为日期时间

上面的代码使用DateTimeFormatter将当前时间格式化为“yyyy-MM-dd HH:mm:ss”的字符串格式。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值