Tung Vo
Tung Vo

Reputation: 2549

Get last date of year java 8

I can see Java 8 new Date Time API has many class LocalDateTime, LocalDate, LocalTime. I can get first date of year something like:

LocalDateTime first = LocalDateTime.of(LocalDate.now().getYear(), 1, 1, 0, 0);

How to get last date of year in Java 8 new Date Time API ?

Upvotes: 2

Views: 1286

Answers (3)

Tung Vo
Tung Vo

Reputation: 2549

After some research and exercise i post some method i have tested

ZonedDateTime lastDateOfYear = LocalDate.now()
        .withYear(year)
        .with(TemporalAdjusters.lastDayOfYear())
        .atStartOfDay(ZoneId.systemDefault());

ZonedDateTime firstDateOfMonth = LocalDate.now()
        .with(TemporalAdjusters.firstDayOfMonth())
        .atStartOfDay(ZoneId.systemDefault());

with LocalDate and LocalDateTime

public static LocalDate getFirstDayOfMonth(LocalDate date) {
    LocalDate firstDayOfMonth = date.withDayOfMonth(1).atTime(0, 0, 0).toLocalDate();
    return firstDayOfMonth;
}


public static LocalDateTime getFirstDayOfMonth(LocalDateTime date) {
    LocalDateTime firstDayOfMonth = date.with(TemporalAdjusters.firstDayOfMonth());
    return firstDayOfMonth;
}

public static LocalDate getFirstDayOfYear(LocalDate date) {
    return date.with(TemporalAdjusters.firstDayOfYear());
}

public static LocalDateTime getFirstDayOfYear(LocalDateTime date) {
    return date.with(TemporalAdjusters.firstDayOfYear())
        .withHour(0).withMinute(0).withSecond(0).withNano(0);
}


public static LocalDate getLastDayOfYear(LocalDate date) {
    return date.with(TemporalAdjusters.lastDayOfYear());
}

public static LocalDateTime getLastDayOfYear(LocalDateTime date) {
    return date.with(TemporalAdjusters.lastDayOfYear())
        .withHour(23).withMinute(59).withSecond(59).withNano(999999999);
}

public static LocalDate getLastDayOfMonth(LocalDate date) {
    LocalDate lastDayOfMonth = date.withDayOfMonth(date.lengthOfMonth()).atTime(0, 0, 0).toLocalDate();
    return lastDayOfMonth;
}

public static LocalDateTime getLastDayOfMonth(LocalDateTime date) {

    LocalDateTime lastDayOfMonth = date.with(TemporalAdjusters.lastDayOfMonth());
    return lastDayOfMonth;
}

public static LocalDateTime getLastDateTimeOfMonth(LocalDateTime date) {

    LocalDateTime lastDayOfMonth = date.with(TemporalAdjusters.lastDayOfMonth())
        .withHour(23).withMinute(59).withSecond(59).withNano(999999999);
    return lastDayOfMonth;
}

Upvotes: 0

Basil Bourque
Basil Bourque

Reputation: 340118

The Answer by Avinash is correct.

Another way to do the same is use of Year and MonthDay classes.

We know the last day of every year is December 31.

MonthDay december31 = MonthDay.of( Month.DECEMBER , 31 ) ;

Apply that month-day (MonthDay) to a year (Year) to determine a date (LocalDate).

Year currentYear = Year.now( ZoneId.of( "Africa/Tunis" ) ) ;
LocalDate lastDateOfCurrentYear = currentYear.atMonthDay( december31 ) ; 

Upvotes: 3

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79580

You can use TemporalAdjusters.lastDayOfYear() with LocalDate#with.

Demo:

import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;

public class Main {
    public static void main(String[] args) {
        System.out.println(LocalDate.now().with(TemporalAdjusters.lastDayOfYear()));
    }
}

Output:

2021-12-31

ONLINE DEMO

Learn more about the modern Date-Time API from Trail: Date Time.

Upvotes: 6

Related Questions