java8 的时间、日期处理类Date —->LocalDate LocalDate—>Date

Java 8引入了新的时间日期API,用于更方便地处理时间和日期。以下是Java 8中常用的时间日期处理类的总结:

  1. LocalDate:用于表示日期。它是不可变且线程安全的类。LocalDate提供了许多方法来获取、操作和格式化日期。
 

// 创建当前日期 LocalDate currentDate = LocalDate.now(); // 获取年、月、日 int year = currentDate.getYear(); int month = currentDate.getMonthValue(); int day = currentDate.getDayOfMonth(); // 操作日期 LocalDate tomorrow = currentDate.plusDays(1); LocalDate oneWeekLater = currentDate.plusWeeks(1);

  1. LocalTime:用于表示时间。它也是不可变且线程安全的类。LocalTime提供了许多方法来获取、操作和格式化时间。
 

// 创建当前时间 LocalTime currentTime = LocalTime.now(); // 获取时、分、秒 int hour = currentTime.getHour(); int minute = currentTime.getMinute(); int second = currentTime.getSecond(); // 操作时间 LocalTime oneHourLater = currentTime.plusHours(1); LocalTime twoMinutesEarlier = currentTime.minusMinutes(2);

  1. LocalDateTime:用于表示日期和时间。它是LocalDateLocalTime的组合。LocalDateTime提供了许多方法来获取、操作和格式化日期和时间。
 

// 创建当前日期和时间 LocalDateTime currentDateTime = LocalDateTime.now(); // 获取年、月、日、时、分、秒 int year = currentDateTime.getYear(); int month = currentDateTime.getMonthValue(); int day = currentDateTime.getDayOfMonth(); int hour = currentDateTime.getHour(); int minute = currentDateTime.getMinute(); int second = currentDateTime.getSecond(); // 操作日期和时间 LocalDateTime oneHourLater = currentDateTime.plusHours(1); LocalDateTime twoDaysEarlier = currentDateTime.minusDays(2);

  1. Instant:用于表示时间戳。它表示从UNIX纪元(1970年1月1日午夜UTC)开始的时间距离,以秒和纳秒为单位。
 

// 获取当前时间戳 Instant currentTimestamp = Instant.now(); // 获取秒数和纳秒 long seconds = currentTimestamp.getEpochSecond(); long nanos = currentTimestamp.getNano();

  1. Duration:用于表示时间间隔。它提供了使用时长来操作时间的方法,例如计算两个时间点之间的差异。
 

// 计算两个时间点之间的时长 LocalDateTime startDateTime = LocalDateTime.of(2022, 1, 1, 12, 0); LocalDateTime endDateTime = LocalDateTime.of(2022, 1, 1, 14, 30); Duration duration = Duration.between(startDateTime, endDateTime); // 获取时长的秒数 long seconds = duration.getSeconds();

  1. Period:用于表示日期间隔。它提供了使用天、月和年来操作日期的方法,例如计算两个日期之间的差异。
 

// 计算两个日期之间的日期间隔 LocalDate startDate = LocalDate.of(2022, 1, 1); LocalDate endDate = LocalDate.of(2022, 2, 1); Period period = Period.between(startDate, endDate); // 获取日期间隔的天数、月数、年数 int days = period.getDays(); int months = period.getMonths(); int years = period.getYears();

  1. DateTimeFormatter:用于格式化和解析日期时间对象。它提供了一些预定义的格式和模式,同时也支持自定义的格式。
 

// 格式化日期时间 LocalDateTime dateTime = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String formattedDateTime = dateTime.format(formatter); // 解析字符串为日期时间 String dateString = "2022-01-01"; LocalDate parsedDate = LocalDate.parse(dateString, DateTimeFormatter.ISO_DATE);

  1. ZoneId 和 ZoneOffset:用于表示时区。ZoneId表示一个区域标识符,例如"Europe/Paris"。ZoneOffset表示相对于协调世界时(UTC)的时差。
 

// 获取默认时区 ZoneId defaultZone = ZoneId.systemDefault(); // 获取指定时区的当前时间 ZoneId parisZone = ZoneId.of("Europe/Paris"); ZonedDateTime parisDateTime = ZonedDateTime.now(parisZone); // 获取时差 ZoneOffset offset = ZoneOffset.ofHours(2);

这些类提供了更加灵活和易用的时间日期处理方式,避免了以前Date类的一些问题和限制。在选择使用哪个类时,取决于您的具体需求,例如只需要表示日期就使用LocalDate,需要表示日期和时间就使用LocalDateTime,等等。

指令

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值