Java LocalDate Class | ofEpochDay() Method with Example

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

LocalDate Class ofEpochDay() method

  • ofEpochDay() method is available in java.time package.
  • ofEpochDay() method is used to create an instance of LocalDate to count the given days from the epoch of 1970-01-01.
  • ofEpochDay() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • ofEpochDay() method may throw an exception at the time of representing days.
    DateTimeException: This exception may throw when the calculated result value exceeds the limit.

Syntax:

    public LocalDate ofEpochDay(long days_val);

Parameter(s):

  • long days_val – represents the day to change into epoch day with the help of epoch format 1970-01-01 (since 1 Jan 1970).

Return value:

The return type of this method is LocalDate, it returns the LocalDate that holds the day value converted the given days based on 1970-01-01.

Example:

// Java program to demonstrate the example 
// of ofEpochDay(long days_val) method of LocalDate

import java.time.*;

public class OfEpochDayOfLocalDate {
    public static void main(String args[]) {
        long ep_day = 60;

        // Here, this method creates an instance 
        // of LocalDate by using the given epoch
        // day count from 1970-01-01 i.e.
        // (1970-01-01 + 60 days = 1970-03-02)
        LocalDate epoch_day = LocalDate.ofEpochDay(60);

        // Display epoch_day
        System.out.println("LocalDate.ofEpochDay(60): " + epoch_day);

        // Here, this method creates an instance
        // of LocalDate by using the given epoch
        // day count from 1970-01-01 i.e.
        // (1970-01-01 + 50 days = 1970-02-20)
        epoch_day = LocalDate.ofEpochDay(50);

        // Display epoch_day
        System.out.println("LocalDate.ofEpochDay(50): " + epoch_day);
    }
}

Output

LocalDate.ofEpochDay(60): 1970-03-02
LocalDate.ofEpochDay(50): 1970-02-20


Comments and Discussions!

Load comments ↻





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