0

I want to get current timestamp in ISO 8601 format with microseconds, like this format:

2021-12-15T16:06:24.400087Z

I tried this but it did not work.

Instant ins = Instant.now().truncatedTo(ChronoUnit.MICROS);

This is the timestamp that I get:

2022-06-27T07:05:13.277Z

andrewJames
  • 19,570
  • 8
  • 19
  • 51
abcd efgh
  • 5
  • 3
  • Does this answer your question? [How to get current moment in ISO 8601 format with date, hour, and minute?](https://stackoverflow.com/questions/3914404/how-to-get-current-moment-in-iso-8601-format-with-date-hour-and-minute) – collapsar Jun 27 '22 at 16:31
  • In which way precisely did not work?? Your attempt is correct, it’s the best you can do. However the precision of the result of calling `now()` depends on the underlying Java version, operating system, hardware and the integration of all of those. You may not be able to get finer than milliseconds or even coarser on your setup. – Ole V.V. Jun 27 '22 at 16:34
  • Welcome to Stack Overflow. Since you’re new it’s OK that you did not know: We can’t really do anything with “did not work”, cannot help you. We need an example of your expected output and an example of how precisely your observed output differed. Please [edit](https://stackoverflow.com/posts/72775433/edit) your question and add this information. So that we may help you. – Ole V.V. Jun 27 '22 at 16:37
  • @OleV.V. this is the timestamp I'm that I timestamp I get ":"2022-06-27T07:05:13.277Z" – abcd efgh Jun 27 '22 at 16:38
  • If I were to tease you, I’d just point out that `2022-06-27T07:05:13.277Z` is the same as `2022-06-27T07:05:13.277000Z`, so we can’t really tell from the output that you didn’t get microseconds. You are correct, of course, it’s still likely that you cannot get a precision beyond milliseconds on your Java. Which Java version are you using? On which OS? – Ole V.V. Jun 27 '22 at 16:40
  • This is the version of java 1.6.0_18 on windows. I make a loop and all the timestamp are like this 2022-06-27T07:05:13.277Z 2022-06-27T07:05:13.278Z 2022-06-27T07:05:13.279Z ... – abcd efgh Jun 27 '22 at 16:43
  • @abcdefgh: are you **sure** that this is on 1.6.0_18? Because `Instant` wasn't introduced until Java 8 (i.e. 1.8.0). Also: why the *heck* would you run such an ancient version of Java? That's a security, stability, compatibility and supportability-risk all in one neat little ancient package! – Joachim Sauer Jun 27 '22 at 16:48
  • Okay I'm will update java right now, yes this is the version I get after running this command java -version. – abcd efgh Jun 27 '22 at 16:51
  • 1
    @JoachimSauer The OP may be using the [ThreeTen Backport](https://www.threeten.org/threetenbp/), the backport of java.time to Java 1.6 and 1.7. abcd efgh, are you importing `java.time.Instant` or `org.threeten.bp.Instant`? – Ole V.V. Jun 27 '22 at 17:34
  • If it’s a matter of getting 6 decimals printed, you may use `OffsetDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSSSSX"))` to get like `2022-06-27T17:36:04.516000Z`. – Ole V.V. Jun 27 '22 at 17:36
  • Thanks for your help @OleV.V. I used your suggestion and it worked, please can you add it as answer so I can mark this question as answered. – abcd efgh Jun 28 '22 at 09:03
  • Glad that it was helpful. I am no longer contributing answers to Stack Overflow (for reasons that I try to explain in [my profile](https://stackoverflow.com/users/5772882/ole-v-v)). Feel free to post it as an answer yourself, and I shall be happy to upvote. You can also accept your own answer after a period (I think it’s 12 hours). – Ole V.V. Jun 28 '22 at 13:41

0 Answers0