LAST UPDATED: NOVEMBER 27, 2020
Java LocalDate toEpochDay() Method
Java toEpochDay() method is used to convert this date to Epoch Day. The Epoch Day count is a simple incrementing count of days where day 0 is 1970-01-01 (ISO). For example, we have a date 1970-01-11 then this method will return 10 epoch-days.
It does not take any argument but returns a long type value that represents the total epoch-days. The syntax of the method is given below.
Syntax
public long toEpochDay()
Parameters:
It does not take any parameter.
Returns:
It returns the Epoch Day equivalent to this date.
Time for an Example:
Let's take an example to get epoch days by using the toEpochDay()
method. Here, we are using a date 1970-01-10 that holds total of 9 epoch days.
import java.time.LocalDate;
public class DateDemo {
public static void main(String[] args){
LocalDate localDate = LocalDate.of(1970, 01, 10);
System.out.println(localDate);
long l = localDate.toEpochDay();
System.out.println("Epoch Days : "+l);
}
}
1970-01-10
Epoch Days : 9
Time for another Example:
Let's take another example to understand the toEpochDay()
method. Here, we have a long range date 2020-01-10 that returns a total of 18271 epoch days.
import java.time.LocalDate;
public class DateDemo {
public static void main(String[] args){
LocalDate localDate = LocalDate.of(2020, 01, 10);
System.out.println(localDate);
long l = localDate.toEpochDay();
System.out.println("Epoch Days : "+l);
}
}
2020-01-10
Epoch Days : 18271
Live Example:
Try with a live example, execute the code instantly with our powerful Online Java Compiler.