tl;dr
Instant.now().toString()
See this code run at Ideone.com.
2022-11-08T07:18:21.482293Z
Instant.now().toString()
As commented by Ole V.V., you can capture the current moment as seen with an offset of zero hours-minutes-seconds from UTC by using the java.time.Instant
class.
Instant instant = Instant.now() ;
ISO 8601
You can generate a string in standard ISO 8601 format, your desired format. by calling Instant#toString()
.
String output = instant.toString() ;
Of course we can combine these:
Instant.now().toString()
Resolution
An Instant
represents a moment in UTC with a resolution as fine as nanoseconds.
Note that Instant#toString
uses a formatter that formats the fractional seconds in groups of three digits, up to nine digits for nanoseconds. Any finer group of all zeros is suppressed. So if your Instant
has only milliseconds with zero microseconds/nanoseconds, you get three digits. If micros without nanos, six digits. If nanos, nine digits.
You will likely capture the current moment with:
- Milliseconds (3 digits in fractional second) in Java 8.
- Microseconds (6 digits) in Java 9 and later, due to a fresh implementation of the
Clock
class in the OpenJDK project’s codebase.
Common computer hardware clocks are not accurate in nanoseconds. So a Instant
can represent a value with nanoseconds, but you'll not likely see the current moment captured with that precision.
If you want only milliseconds, truncate. Specify your desired granularity by way of ChronoUnit
enum, and implementation of TemporalUnit
.
Instant.now().truncatedTo( ChronoUnit.MILLIS ).toString()
But remember, if you happened to have a fractional second of zero, no fractional second digits at all would appear in the resulting String
object.
Z
The Z
on the end of your desired format means an offset of zero. Pronounced “Zulu”. Comes from aviation/military history. Standardized in ISO 8601.
Avoid legacy date-time classes
You said:
I tried using the zoneddatetime and simple date format
The java.time.ZonedDateTime
class is for representing a moment as seen in a particular time zone. Your desired format has no time zone, only an offset of zero. So ZonedDateTime
is not appropriate here. Use Instant
instead, or use OffsetDateTime
if you need more flexibility such as other formatters.
Definitions:
- An offset is merely a number of hours-minutes-seconds ahead or behind the temporal prime meridian of UTC.
- A time zone is much more. A time zone is a named history of the past, present, and future changes to the offset used by the people of a particular region as decided by their politicians.
Never use the terribly flawed classes of SimpleDateFormat
, Date
, Calendar
, etc. These are now legacy since Java 8+, supplanted by the modern java.time classes defined in JSR 310.
Instant.now().toString()
(yes, if you accept not having control over the number of decimals, it really is this simple). Leave out.toString()
if you do not require aString
. – Kislev.844
)? Asking because your format is ISO 8601 and that standard doesn’t put a limit on the number of decimals. – Kislev