Java8的日期时间API LocalDate LocalTime LocalDateTime Period... Introduction to the Java 8 Date/Time API

本文详细介绍了Java8的日期时间API,包括LocalDate, LocalTime, LocalDateTime的使用,以及如何通过Period和Duration进行日期时间计算。Java8的新API解决了旧版Date和Calendar的线程安全问题和设计不足,提供了更好的日期时间处理体验。文章还探讨了ZonedDateTime,以及与旧API的兼容性和日期时间格式化。" 132953109,20038732,Kafka实战:高效数据推送指南,"['分布式系统', '消息队列', 'Kafka实践', '数据处理', '实时数据']
摘要由CSDN通过智能技术生成

原文链接:Introduction to the Java 8 Date/Time API

1. Overview 概述

Java 8 introduced new APIs for Date and Time to address the shortcomings of the older java.util.Date and java.util.Calendar.

Java8引入了新的日期时间API向旧的 java.util.Date 和 java.util.Calendar 挑衅、讨伐。

In this tutorial, let's start with the issues in the existing Date and Calendar APIs and discuss how the new Java 8 Date and Time APIs address them.

在这篇文章中,让我们一起先讨论下旧的日期日历APIs,之后围观下Java8中新的日期时间APIs是如何向旧社会挑战的。

We will also look at some of the core classes of the new Java 8 project that are part of the java.time package, such as LocalDateLocalTime, LocalDateTime, ZonedDateTime, Period, Duration and their supported APIs.

我们也会聚焦于Java8中的 java.time 类包和其中的一些主要类,如 LocalDate LocalDateTime, ZonedDateTime Period Duration 和这些类中的APIs。

2. Java8之前的日期API的潜在问题 Issues With the Existing Date/Time APIs

  • Thread safety – The Date and Calendar classes are not thread safe, leaving developers to deal with the headache of hard-to-debug concurrency issues and to write additional code to handle thread safety. On the contrary, the new Date and Time APIs introduced in Java 8 are immutable and thread safe, thus taking that concurrency headache away from developers.

        线程安全问题:java.util.Date 和 java.util.Calendar 不是线程安全的,它俩让开发人员来处理既头疼又很难调试的线程同步问题,为此还要去编写额外的代码来保证线程安全。相反的,Java8中新的日期时间API是不可变且线程安全的,因此开发人员不用再头疼线程并发问题啦。

  • API design and ease of understanding – The Date and Calendar APIs are poorly designed with inadequate methods to perform day-to-day operations. The new Date/Time API is ISO-centric and follows consistent domain models for date, time, duration and periods. There are a wide variety of utility methods that support the most common operations.

        API的设计与通俗易懂:旧的日期日历函数设计不够好,没有足够的方法来完成某天到某天的计划等操作。而Java8的日期函数遵循了ISO为中心的模式,基于不变的 日期、时间、间隔时间段等等,设计了一堆的公共方法来的操作日期变换。

  • ZonedDate and Time – Developers had to write additional logic to handle time-zone logic with the old APIs, whereas with the new APIs, handling of time zone can be done with Local and ZonedDate/Time APIs.

        时区及时间:在之间的开发中,开发人员不得不编写额外的逻辑来处理有关时区的时间操作。而在Java8的时间函数中,时区处理只需要使用Local类和ZoneDateTime的API就可。

  

3. 使用Local相关类 Using LocalDateLocalTime and LocalDateTime

The most commonly used classes are LocalDateLocalTime and LocalDateTime. As their names indicate, they represent the local date/time from the context of the observer.

Java8中最常被使用的日期函数类是 LocalDate LocalTime LocalDateTime这三个,就像它们的名字那样,分别代表了要操作的本地 日期 时间 及 日期时间。

We mainly use these classes when time zones are not required to be explicitly specified in the context. As part of this section, we will cover the most commonly used APIs.

在时区没有要求要明确指定出来的情况下,我们主要使用的就是以上这三个日期时间操作类。

  

3.1. Working With LocalDate 使用 LocalDate类

The LocalDate represents a date in ISO format (yyyy-MM-dd) without time. We can use it to store dates like birthdays and paydays.

LocalDate类代表了ISO格式(yyyy-MM-dd)的日期对象,使用它可以来存储像生日和账单日类型的数据。

An instance of current date can be created from the system clock:

下面是一个从系统时钟为初始值来创建 LocalDate 的例子,代码如下:

LocalDate localDate = LocalDate.now();

And we can get the LocalDate representing a specific day, month and year by using the of method or the parse method.

也可以通过使用 LocalDate 的 parse 方法指定年月日的形式来创建 LocalDate 实例。

For example, these code snippets represent the LocalDate for February 20, 2015:

例如,下面代码就创建了一个 2015年2月20号 的日期对象:

LocalDate.of(2015, 02, 20);

LocalDate.parse("2015-02-20");

The LocalDate provides various utility methods to obtain a variety of information. Let's have a quick peek at some of these API methods.

LocalDate类提供了许多工具方法来获取信息,接下来看看这些工具方法:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值