LocalDate ofEpochDay() method in Java with Examples

The ofEpochDay(long epochDay) method of LocalDate class in Java is used to obtain an instance of LocalDate from the epoch day count. Epoch day is 01-01-1970(DD-MM-YYYY). This is considered as a start of the epoch day. The method returns the LocalDate by adding the passed days into Epoch date i.e. 01-01-1970. Suppose 2 is passed as a parameter, the method will return 03-01-1970 (2 is added to ’01’ from the epoch day(DD)). Similarly, if 365 is passed then a whole new year will be added to the epoch date.

Syntax:

public static LocalDate ofEpochDay(long epochDay)

Parameters: This method accepts one parameter epochDay which is the conversion base.

Return Value: This method returns the localdate after conversion.

Exceptions: This method throws DateTimeException if the epoch day exceeds the supported date range.



Below programs illustrate the ofEpochDay(long epochDay) method in Java:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// LocalDate.ofEpochDay(long epochDay) method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // Create LocalDate object
        LocalDate localdate
            = LocalDate.ofEpochDay(100);
  
        // Display full date
        System.out.println("Date: "
                           + localdate);
    }
}

chevron_right


Output:

Date: 1970-04-11

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// LocalDate.ofEpochDay(long epochDay) method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // Create LocalDate object
        LocalDate localdate
            = LocalDate.ofEpochDay(365);
  
        // Display date
        System.out.println("Date: "
                           + localdate);
    }
}

chevron_right


Output:

Date: 1971-01-01

References:
https://docs.oracle.com/javase/10/docs/api/java/time/LocalDate.html#ofEpochDay(long)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.