1

in Jackson JavaTimeModule java time objects are converted to timestamps in the for of 1421261297.356000000, where the integer part is the number of seconds and the decimal part the number of nanoseconds of an Instant.

I want to convert this back to a ZonedDateTime, so the first step would be to convert this timestamp string into an Instant, but this format is completely out of standard and thus, there isn't any java.time method (that I know of) to convert 1421261297.356000000 to an Instant directly (without losing the nanosecond information).

I could do some string manipulation and extract the seconds and add the nanoseconds later, but it would be nice to not reinvent the wheel with this kind of boilerplate code, so my question is: Are there any standard ways, methods or classes, to convert a Jackson JavaTimeModule timestamp string to an Instant?`

Michel Feinstein
  • 13,416
  • 16
  • 91
  • 173
  • Why don't you parse the JSON with Jackson in the first place? It will parse the value to an Instant for you. – JB Nizet Jan 07 '18 at 18:52
  • Because I am using it on Android with `Retrofit` + `Moshi` on the App, and `Jersey` + `Jackson` on the webserver. I could change it to use `Jackson` with `Retrofit`, but I want to stick with `Moshi` for simplicity and better integration with `Retrofit` – Michel Feinstein Jan 07 '18 at 19:13
  • Checked [this](https://stackoverflow.com/q/27952472/6413377) already if it is any help? – pirho Jan 07 '18 at 20:04
  • This only works if I was using Jackson, but I am using Moshi. – Michel Feinstein Jan 07 '18 at 20:43
  • This has been asked and answered a couple of times already. Please exercise your search skills. I have not heard of a standard method for it, but it is pretty straightforward to code. – Ole V.V. Jan 08 '18 at 09:18
  • Possible duplicate of [Cannot convert from java Double to java Date](https://stackoverflow.com/questions/47701927/cannot-convert-from-java-double-to-java-date) – Ole V.V. Jan 08 '18 at 09:18
  • Possible duplicate of [converting epoch to ZonedDateTime in Java](https://stackoverflow.com/questions/47975195/converting-epoch-to-zoneddatetime-in-java) (the answers there do a little more than you ask for, but you can just take out the relevant part). – Ole V.V. Jan 08 '18 at 09:24
  • 1
    You could also configure Jackson to emit an ISO-8601 offset datetime. – Mark Rotteveel Jan 08 '18 at 09:38

0 Answers0