i want to get the 25 hours in miliseconds i.e. 90000000 (25 hours in ms)
is there any way i can calculate dynamically using Java 8 Date and Time api ?
i am trying like below:
LocalDate.now().minusHours(25).toEpochDay()
but seems compilation error::
The method minusHours(int) is undefined for the type LocalDate
>Solution :
The following gives you 25 hours converted into milliseconds:
Duration.ofHours(25).toMillis()
If you want this much milliseconds behind the Unix epoch, you can subtract this duration from Instant.EPOCH using Instant.EPOCH.minus.
Demo:
import java.time.Duration;
import java.time.Instant;
public class SecondLargest {
public static void main(String[] args) {
System.out.println(Duration.ofHours(25).toMillis());
System.out.println(Instant.EPOCH.minus(Duration.ofHours(25)));
System.out.println(Instant.EPOCH.minus(Duration.ofHours(25)).toEpochMilli());
}
}
Output:
90000000
1969-12-30T23:00:00Z
-90000000
Learn more about the the modern date-time API from Trail: Date Time.