Epoch in Squirrel
Here’s the translated code and explanation in Markdown format suitable for Hugo:
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 Java, we use the Instant
class from the java.time
package to work with moments in time. The Instant.now()
method gives us the current time, which we can then use to get the number of seconds, milliseconds, or nanoseconds since the Unix epoch.
The getEpochSecond()
method returns the number of seconds since the epoch, toEpochMilli()
returns milliseconds, and getNano()
returns the nanosecond adjustment to the second.
We can also create an Instant
from a given number of seconds (and optionally, nanoseconds) since the epoch using Instant.ofEpochSecond()
.
Next, we’ll look at another time-related task: time parsing and formatting.