tl;dr
java.time.Instant // Represent a moment as seen with an offset-from-UTC of zero hours-minutes-seconds.
.now() // Capture the current moment.
.truncatedTo( // Lop off any fractional second.
ChronoUnit.SECONDS // An enum specifying the granularity of truncation.
) // Returns another `Instant` object rather than altering the original, per immutable-objects pattern.
.toString() // Generate text representing the content of this object, using standard ISO 8601 format.
2021-05-25T01:05:03Z
Details
Avoid legacy date-time classes
You are using terrible date-time classes that were supplanted years ago by the modern java.time classes.
java.time
To capture the current moment as seen in UTC, use Instant.now
.
So:
Instant.now().toString()
… is all you need.
See this code run live at IdeOne.com.
2021-05-25T01:05:03.208937Z
The Z
on the end means an offset-from-UTC of zero hours-minutes-seconds. Pronounced “Zulu”. Equivalent to +00:00
.
Truncation
If you just want the whole seconds only, and drop the fractional second, truncate.
Instant.now().truncatedTo( ChronoUnit.SECONDS ).toString()
See the code run live at IdeOne.com.
2021-05-25T01:05:03Z