Search code examples
javamillisecondstemporal

How to convert System.currentMillisSeconds to a TemporalAccessor


I need to convert a standard long System.currentmillis to a temporal accessor and have no clue how to even begin.


Solution

  • Instant is a TemporalAccessor, so you can create an Instant from a number of milliseconds since the epoch:

    TemporalAccessor ta = Instant.ofEpochMilli(System.currentTimeMillis());
    

    Note that the docs for System.currentTimeMillis says that the granularity of the value depends on the OS, so it might not be the exact time in milliseconds.

    Returns the current time in milliseconds. Note that while the unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds.