Extracting Year, Month and Day from Date in Java – 在Java中从日期中提取年、月、日

最后修改: 2018年 6月 30日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this short tutorial, we’ll look at how to extract the year, month, and day from a given Date in Java.

在这个简短的教程中,我们将看看如何在Java中从一个给定的Date中提取

We’ll be looking at how to extract these values using the legacy java.util.Date class and also by using the new date-time library of Java 8.

我们将研究如何使用传统的java.util.Date类以及使用Java 8的新的日期时间库来提取这些值。

In Java 8, a whole new date and time library was introduced for a number of good reasons. Besides other advantages, the new library provides a better API for operations such as extracting Year, Month, Day etc. from given Date.

在Java 8中,由于一些好的理由,引入了一个全新的日期和时间库。除了其他优点外,新库为诸如从给定的Date中提取YearMonthDay等操作提供了更好的 API。

And, if you’re looking for a more detailed article on the new date-time library, have a look here.

而且,如果你正在寻找关于新的日期时间库的更详细的文章,请看看这里

2. Using LocalDate

2.使用LocalDate

The new java.time package contains a number of classes that can be used for representing Date.

新的java.time包包含一些可用于表示Date的类。

Each class differs by the additional information it stores in addition to the Date.

每个类的不同之处在于,除了Date之外,它还储存了额外的信息。

The basic LocalDate just contains the date information, while LocalDateTime contains date as well as time information.

基本的LocalDate只包含日期信息,而LocalDateTime包含日期以及时间信息。

Similarly, more advanced classes such as OffsetDateTime and ZonedDateTime contains additional information about offset from UTC and information about time-zone respectively.

同样,更高级的类,如OffsetDateTimeZonedDateTime分别包含从UTC偏移的额外信息和时区的信息。

In any case, all these classes support direct methods to extract Year, Month and Date information.

在任何情况下,所有这些类都支持直接提取年、月和日期信息的方法。

Let’s explore these methods to extract information from a LocalDate instance name localDate.

让我们来探索这些方法,从一个LocalDate实例名称localDate中提取信息。

2.1. Get Year

2.1.获取年份

To extract Year, LocalDate simply provides a getYear method:

为了提取年,LocalDate仅仅提供了一个getYear方法。

localDate.getYear();

2.2. Get Month

2.2.获得月度

Similarly, to extract Month, we use getMonthValue API:

同样,为了提取Month,我们使用getMonthValueAPI。

localDate.getMonthValue();

Unlike Calendar, Months in LocalDate are indexed from 1; for January this will return 1.

Calendar不同,LocalDate中的月份是从1开始索引的;对于1月,这将返回1。

2.3. Get Day

2.3.获得日

Finally, to extract Day, we have getDayOfMonth method:

最后,为了提取Day,我们有getDayOfMonth方法。

localDate.getDayOfMonth();

3. Using java.util.Date

3.使用java.util.Date

For a given java.util.Date to extract individual fields such as Year, Month, Day etc. the first step we need to do is to convert it to Calendar instance:

对于一个给定的java.util.Date提取单个字段,如Year, Month, Day等,我们需要做的第一步是将其转换成Calendar实例。

Date date = // the date instance
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);

Once we have a Calendar instance, we can directly invoke its get method and provide the specific field that we want to extract.

一旦我们有了一个Calendar 实例,我们就可以直接调用它的get方法并提供我们想要提取的特定字段。

We can use constants present in Calendar to extract specific fields.

我们可以使用Calendar中的常量来提取特定的字段。

3.1. Get Year

3.1.获取年份

To extract the year, we can invoke get by passing Calendar.YEAR as an argument:

为了提取年,我们可以通过传递Calendar.YEAR作为一个参数来调用get

calendar.get(Calendar.YEAR);

3.2. Get Month

3.2.获得月度

Similarly to extract the month, we can invoke get by passing Calendar.MONTH as an argument:

同样地,为了提取月,我们可以通过传递Calendar.MONTH作为一个参数来调用get

calendar.get(Calendar.MONTH);

Please note that months in Calendar are zero-indexed; for January this method will return 0.

请注意,Calendar中的月份是零指数的;对于1月,这个方法将返回0。

3.3. Get Day

3.3.获得日

Finally, to extract the day, we invoke get by passing Calendar.DAY_OF_MONTH as an argument:

最后,为了提取日期,我们通过传递Calendar.DAY_OF_MONTH作为一个参数来调用get

calendar.get(Calendar.DAY_OF_MONTH);

4. Conclusion

4.总结

In this quick tutorial, we’ve explored how to extract integer values of Year, Month and Day from Date in Java.

在这个快速教程中,我们探讨了如何在Java中从Date中提取YearMonthDay的整数值。

We’ve shown how to extract these values using the old Date and Calendar classes as well as the new date-time library of Java8.

我们已经展示了如何使用旧的DateCalendar类以及Java8的新日期-时间库来提取这些值。

The complete source code for snippets used in this tutorial is available over at Github.

本教程中使用的片段的完整源代码在Github上提供。