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 Groovy.
To run the program, save it as epoch.groovy
and use the groovy
command:
Use Instant.now()
to get the current timestamp. You can then use epochSecond
, toEpochMilli()
, or nano
to get elapsed time since the Unix epoch in seconds, milliseconds, or nanoseconds, respectively.
You can also convert integer seconds or nanoseconds since the epoch into the corresponding Instant
using Instant.ofEpochSecond()
.
Next, we’ll look at another time-related task: time parsing and formatting.