java date beginning of day

java

To get the beginning of the day (midnight) for a java.time.LocalDate object, you can use the atStartOfDay() method of the java.time.LocalDate class.

This method returns a java.time.LocalDateTime object that represents the start of the day (midnight) in the local time zone.

Here’s an example:

import java.time.LocalDate;
import java.time.LocalDateTime;

public class Main {
    public static void main(String[] args) {
        LocalDate date = LocalDate.now();
        System.out.println("Current date: " + date);

        // Get the beginning of the day (midnight)
        LocalDateTime startOfDay = date.atStartOfDay();
        System.out.println("Start of day: " + startOfDay);
    }
}

If you want to get the beginning of the day (midnight) in a specific time zone, you can use the atStartOfDay(ZoneId zone) method and pass a java.time.ZoneId object representing the time zone.

Here’s an example:

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;

public class Main {
    public static void main(String[] args) {
        LocalDate date = LocalDate.now();
        System.out.println("Current date: " + date);

        // Get the beginning of the day (midnight) in the GMT time zone
        ZoneId gmtZone = ZoneId.of("GMT");
        LocalDateTime startOfDay = date.atStartOfDay(gmtZone);
        System.out.println("Start of day in GMT: " + startOfDay);
    }
}