How to Get the Start and the End of a Day using Java – 如何用Java获取一天的开始和结束时间

最后修改: 2018年 6月 29日

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

1. Overview

1.概述

In this short tutorial, we’ll learn how to get the start and the end of a day in Java, using simple, straightforward examples for different scenarios.

在这个简短的教程中,我们将学习如何在Java中获得一天的开始和结束时间,使用简单、直接的例子来说明不同的情况。

We’ll be using the Java’s 8 Date/Time API to build these examples.

我们将使用Java的8个日期/时间API来构建这些例子。

In case you want to read a little bit more about Java’s 8 Date and Time library before proceeding, you can get started here.

如果你想在继续之前多读一点关于Java的8号日期和时间库的内容,你可以从这里开始

2. From a LocalDate Object

2.来自一个LocalDate对象

First of all, let’s see how we can get the start or end of a day given to us as a LocalDate object, such as:

首先,让我们看看如何获得作为LocalDate对象给我们的一天的开始或结束时间,例如。

LocalDate localDate = LocalDate.parse("2018-06-23");

2.1. atStartOfDay()

2.1 atStartOfDay()

The simplest way of getting a LocalDateTime representing the beginning of a particular day is by using the atStartOfDay() method:

获取代表某一天开始的LocalDateTime的最简单方法是使用atStartOfDay()方法。

LocalDateTime startOfDay = localDate.atStartOfDay();

This method is overloaded, thus in case we want to get a ZonedDateTime from it, we can do so by specifying the ZoneId:

这个方法是重载的,因此万一我们想从中获得一个ZonedDateTime,我们可以通过指定ZoneId来实现。

ZonedDateTime startOfDay = localDate.atStartOfDay(ZoneId.of("Europe/Paris"));

2.2. of()

2.2. of()

Another way in which we can achieve the same result is by using the of() method, providing a LocalDate and one of the LocalTime‘s static fields:

另一种我们可以实现相同结果的方法是使用of()方法,提供一个LocalDateLocalTime的静态字段之一。

LocalDateTime startOfDay = LocalDateTime.of(localDate, LocalTime.MIDNIGHT);

LocalTime offers the following static fields: MIDNIGHT (00:00), MIN (00:00), NOON (12:00), and MAX(23:59:59.999999999).

LocalTime提供以下静态字段。MIDNIGHT (00:00), MIN (00:00), NOON (12:00), 和MAX(23:59:59.9999999)。

Therefore, if we want to get the end of the day, we’d use the MAX value.

因此,如果我们想得到一天的结束时间,我们会使用MAX值。

Let’s try it out, but with a different method.

让我们来试一试,但要用不同的方法。

2.3. atTime()

2.3.atTime()

This method is overloaded, allowing us to specify the desired time using hours, minutes, seconds or even nanoseconds.

这个方法是重载的,允许我们用小时、分钟、秒甚至纳秒来指定所需时间。

In this case, anyway, we’ll use the LocalTime‘s MAX field as the argument to get the last moment of the given day:

无论如何,在这种情况下,我们将使用LocalTimeMAX字段作为参数,以获得给定日期的最后时刻。

LocalDateTime startOfDay = localDate.atTime(LocalTime.MAX);

2.4. atDate()

2.4. atDate()

This example is quite similar to the previous ones, but this time, we’ll use the atDate() method of a LocalTime object, passing the LocalDate as the argument:

这个例子与之前的例子很相似,但这次我们将使用LocalTime对象的atDate()方法,传递LocalDate作为参数。

LocalDateTime endOfDate = LocalTime.MAX.atDate(localDate);

3. From a LocalDateTime Object

3.来自一个LocalDateTime对象

It almost goes without saying that we can get the LocalDate from it, and then use any of the methods of section 2 to get the end or start of day from it:

不言而喻,我们可以从中获取LocalDate,然后使用第2节的任何方法从中获取日终或日始。

LocalDateTime localDateTime = LocalDateTime
  .parse("2018-06-23T05:55:55");
LocalDateTime endOfDate = localDateTime
  .toLocalDate().atTime(LocalTime.MAX);

But in this section, we’ll analyze one other method to obtain the object with its time section set to the start or end of the day, directly from another given LocalDateTime object.

但在本节中,我们将分析另一种方法,直接从另一个给定的LocalDateTime对象中获得其时间部分设置为一天的开始或结束的对象。

3.1. with()

3.1.with()

All classes implementing the Temporal interface can use this method.

所有实现时态接口的类都可以使用这个方法

In this case, we’ll use the signature of the method which takes a TemporalField (notably, one of the ChronoField Enum values) and a long argument as the new value of the field:

在这种情况下,我们将使用方法的签名,该方法接收一个TemporalField(特别是ChronoField Enum中的一个值)和一个longargument作为该领域的新值。

LocalDateTime endOfDate = localDateTime.with(ChronoField.NANO_OF_DAY, LocalTime.MAX.toNanoOfDay());

4. From a ZonedDateTime Object

4.来自一个ZonedDateTime对象

If we’re given a ZonedDateTime, we can use the with() method since it implements the Temporal interface as well:

如果我们得到了一个ZonedDateTime,我们可以使用with()方法,因为它也实现了Temporal接口

ZonedDateTime startofDay = zonedDateTime.with(ChronoField.HOUR_OF_DAY, 0);

5. Conclusion

5.总结

To sum up, we’ve analyzed a lot of different ways of getting the start and end of a day in Java for many different case scenarios.

总而言之,我们已经分析了在Java中获得一天的开始和结束的许多不同方式,用于许多不同的案例场景。

Finally, we’ve learned about the insights of Java’s 8 Date and Time library classes and got familiar with many of its classes and interfaces.

最后,我们已经了解了Java的8个日期和时间库类的见解,熟悉了其中的许多类和接口。

As always, all the examples can be accessed in our GitHub repository.

一如既往,所有的例子都可以在我们的GitHub资源库中获取。