java get end of date

java get end of date

To get the end of a date in Java, you can use the LocalDateTime class from the java.time package. This class represents a date and time, and provides methods for working with dates and times.

Here's an example of how to use the LocalDateTime class to get the end of a date:

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class Main {
  public static void main(String[] args) {
    LocalDateTime date = LocalDateTime.of(2022, 12, 31, 23, 59, 59);
    ZoneId zoneId = ZoneId.systemDefault();
    ZonedDateTime endOfDate = date.atZone(zoneId).withLaterOffsetAtOverlap();
    System.out.println(endOfDate); // prints the end of the date in the system's default time zone
  }
}
Sou‮ww:ecr‬w.lautturi.com

In this example, a LocalDateTime object is created with the date "2022-12-31" and the time "23:59:59". The atZone() method is then called on the LocalDateTime object to convert it to a ZonedDateTime object in the system's default time zone. The withLaterOffsetAtOverlap() method is called on the ZonedDateTime object to get the end of the date, which is the last moment of the day in the time zone. The resulting ZonedDateTime object is then printed to the console.

You can use the various methods of the LocalDateTime and ZonedDateTime classes to manipulate the date and time, such as plusDays() to add or subtract days, or withHour() to set the hour.

Created Time:2017-11-03 22:21:09  Author:lautturi