Java LocalDate Class | atStartOfDay() Method with Example

LocalDate Class atStartOfDay() method: Here, we are going to learn about the atStartOfDay() method of LocalDate Class with its syntax and example.
Submitted by Preeti Jain, on June 03, 2020

LocalDate Class atStartOfDay() method

Syntax:

public LocalDateTime atStartOfDay();
public ZonedDateTime atStartOfDay(ZoneId zo);
  • atStartOfDay() method is available in java.time package.
  • atStartOfDay() method is used to create a LocalDateTime at the start of this LocalDate by merging this date with the midnight time.
  • atStartOfDay(ZoneId zo) method is used to create a ZonedDateTime from this LocalDate object at the earliest valid time based on the rules in the given zone.
  • These methods don’t throw an exception at the time of returning the start of the day.
  • These are non-static methods and it is accessible with class objects and if we try to access these methods with the class name then we will get an error.

Parameter(s):

  • In the first cases, atStartOfDay(),
    • None
  • In the second cases, atStartOfDay(ZoneId zo),
    • ZoneId zo – represents the zone.

Return value:

In the first case, the return type of the method is LocalDateTime, it returns LocalDateTime that holds the value midnight time at the start of this LocalDate object.

In the second case, the return type of the method is ZonedDateTime, it returns ZonedDateTime that holds the value from this date at the earliest time in the given zone.

Example:

// Java program to demonstrate the example 
// of atStartOfDay() method of LocalDate

import java.time.*;

public class AtStartOfDayOfLocalDate {
    public static void main(String args[]) {
        // Instantiates two LocalDate
        LocalDate l_da1 = LocalDate.parse("2007-04-04");
        LocalDate l_da2 = LocalDate.now();

        // Instantiates a zone for LocalDate l_da2
        ZoneId z_id = ZoneId.of("Africa/Accra");

        // Display l_da1,l_da2
        System.out.println("LocalDate l_da1 and l_da2: ");
        System.out.println("l_da1: " + l_da1);
        System.out.println("l_da2: " + l_da2);

        System.out.println();

        // Here, this method creates a LocalDateTime
        // by merging this date (l_da1) with 
        // the time of midnight at the start 
        // of this date l_da1
        LocalDateTime da_ti = l_da1.atStartOfDay();

        // Display da_ti
        System.out.println("l_da1.l_da1.atStartOfDay(): " + da_ti);

        // Here, this method creates a ZonedDateTime
        // from this date (l_da2) at the earliest
        // the valid time according to the rules
        // in the given time-zone (z_id) 
        ZonedDateTime zo_da_ti = l_da2.atStartOfDay(z_id);

        // Display zo_da_ti
        System.out.println("l_da2.atStartOfDay(z_id): " + zo_da_ti);
    }
}

Output

LocalDate l_da1 and l_da2: 
l_da1: 2007-04-04
l_da2: 2020-06-03

l_da1.l_da1.atStartOfDay(): 2007-04-04T00:00
l_da2.atStartOfDay(z_id): 2020-06-03T00:00Z[Africa/Accra]


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.