How to obtain the end of the day when given a LocalDate?

JavaJava Time

Java Problem Overview


How to obtain the end of the day when given a LocalDate?

I could get it by doing

LocalDateTime.of(LocalDate.now(), LocalTime.of(23, 59, 59));

But is there an equivalent 'atStartOfDay' method for the end of the day?

LocalDate.now().atStartOfDay();
LocalDate.now().atEndOfDay(); //doesn't work

Java Solutions


Solution 1 - Java

Here are a few alternatives, depending on what you need:

LocalDate.now().atTime(23, 59, 59);     //23:59:59
LocalDate.now().atTime(LocalTime.MAX);  //23:59:59.999999999

But there is no built-in method.

As commented by @JBNizet, if you want to create an interval, you can also use an interval up to midnight, exclusive.

Solution 2 - Java

These are the variants available in LocalTime, notice MIDNIGHT and MIN are equal.

LocalDate.now().atTime(LocalTime.MIDNIGHT); //00:00:00.000000000
LocalDate.now().atTime(LocalTime.MIN);      //00:00:00.000000000
LocalDate.now().atTime(LocalTime.NOON);     //12:00:00.000000000
LocalDate.now().atTime(LocalTime.MAX);      //23:59:59.999999999

For reference, this is the implementation in java.time.LocalTime

/**
 * Constants for the local time of each hour.
 */
private static final LocalTime[] HOURS = new LocalTime[24];
static {
    for (int i = 0; i < HOURS.length; i++) {
        HOURS[i] = new LocalTime(i, 0, 0, 0);
    }
    MIDNIGHT = HOURS[0];
    NOON = HOURS[12];
    MIN = HOURS[0];
    MAX = new LocalTime(23, 59, 59, 999_999_999);
}

If the value assigned to the 4th constructor argument (999_999_999) of LocalTime (representing nanoOfSecond) looks unfamiliar it's because it's making use of the Java 7 feature Underscores in Numeric Literals.

> In Java SE 7 and later, any number of underscore characters (_) can appear anywhere between digits in a numerical literal. This feature enables you, for example, to separate groups of digits in numeric literals, which can improve the readability of your code.

Solution 3 - Java

Get start of next day and subtract 1 second from it. This should work for you. :

public static void main(String[] args) {

	LocalDate date = LocalDate.now();
	LocalDateTime dt = date.atStartOfDay().plusDays(1).minusSeconds(1);
	System.out.println(dt);
}

O/P :

2016-04-04T23:59:59

Solution 4 - Java

public static long getStartOfDay(String country) {
    return LocalDate.now().atStartOfDay(ZoneId.of("Europe/Berlin")).toInstant().toEpochMilli();
}

public static long getEndOfDay(long startOfDay) {
    return startOfDay + 86399000L;  // adding 24h = 1day seconds - 1
}

public static long getStartOfDay(LocalDate localDate, String country) {
    return localDate.atStartOfDay(ZoneId.of("Europe/Berlin")).toInstant().toEpochMilli();
}

public static long getEndOfDay(LocalDate localDate, String country) {
    localDate = localDate.plusDays(1L);
    return localDate.atStartOfDay(ZoneId.of("Europe/Berlin")).toInstant().toEpochMilli() - 1000L; //substract mili
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncooxieView Question on Stackoverflow
Solution 1 - JavaassyliasView Answer on Stackoverflow
Solution 2 - JavaRobert HuntView Answer on Stackoverflow
Solution 3 - JavaTheLostMindView Answer on Stackoverflow
Solution 4 - JavaRavi ParekhView Answer on Stackoverflow