Title here
Summary here
A common requirement in programs is getting the number of seconds, milliseconds, or nanoseconds since the Unix epoch. Here’s how to do it in Java.
To run the program:
In this Java example, we use the Instant
class from the java.time
package to work with instants on the timeline. The Instant.now()
method gives us the current time, and we can use various methods to get the time in different formats:
getEpochSecond()
returns the number of seconds from the epochtoEpochMilli()
returns the number of milliseconds from the epochgetNano()
returns the nanosecond-of-secondWe can also create Instant
objects from epoch seconds or nanoseconds using Instant.ofEpochSecond()
.
Next, we’ll look at another time-related task: time parsing and formatting.