0

Today we have the date/time in epoch format "/Date(16747622680000)/" which can be easy converted into yyyyMMdd:hhmmss when getting the digits as milliseconds from the String and pass it to an instance of java.util.Date today = new Date(16747622680000); and then use a java.text.SimpleDateFormat instance to get the expected result.

But what does the "+0000" in "/Date(253402214400000+0000)/" mean and how to convert that value?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Just a bit of a guess, but it seems like it would be a time zone offset value (maybe `ZZZZ`) – cjstehno Jan 26 '23 at 20:49
  • 1
    *"and then use a `java.text.SimpleDateFormat` instance to get the expected result"* and, no, don't do that. Instead make use of the newer and improved `java.time` APIs – MadProgrammer Jan 26 '23 at 21:23
  • 1
    `new Date(16747622680000)` and again, no, don't do that. Use `LocalDateTime.now()` or `ZonedDateTime.now()` instead – MadProgrammer Jan 26 '23 at 21:27
  • 1
    Did you really mean 14 digits in your example input of `16747622680000`? Or is that an typo, one too many zero digits on the end? – Basil Bourque Jan 26 '23 at 21:49

1 Answers1

3

tl;dr

The +0000 means an offset from UTC of zero hours-minutes-seconds.

(Deleted last digit zero (0) of example input, presumed to be a typo)

Instant
.ofEpochMilli( 
    "/Date(1674762268000)/".replace( "/" , "" ).replace( "Date(" , "" ).replace( ")" , "" ).replace( "/" , "" )
)
.toString()

See this code run at IdeOne.com.

2023-01-26T19:44:28Z

The Z on the end and the +0000 seen in your Question, both mean an offset from UTC of zero hours-minutes-seconds.

java.time

an instance of java.util.Date today = new Date(16747622680000); and then use a java.text.SimpleDateFormat

No, never use Date & SimpleDateFormat. These classes are now legacy and are terribly flawed, designed by people who did not understand date-time handling. They were years ago supplanted by the modern java.time classes defined in JSR 310.

we have the date/time in epoch format "/Date(16747622680000)/"

First, clean your input string to get only digits.

Also, I assume you made a typo, with an extra zero digit at the end. I removed that digit.

String input = "/Date(1674762268000)/";
String inputDigits = input.replace( "/" , "" ).replace( "Date(" , "" ).replace( ")" , "" ).replace( "/" , "" );
long countFromEpoch1970Utc = Long.parseLong( inputDigits );

At this point, we have a long integer number with the value 1_674_762_268_000L.

countFromEpoch1970Utc = 1674762268000

Parse that number as a count of milliseconds since the epoch reference of the first moment of 1970 as seen with an offset from UTC of zero hours-minutes-seconds, 1970-01-01T00:00Z.

Instant instant = Instant.ofEpochMilli( countFromEpoch1970Utc );

See this code run at IdeOne.com.

instant.toString(): 2023-01-26T19:44:28Z

ISO 8601

The text in the string 2023-01-26T19:44:28Z is in a format defined by the ISO 8601 standard. I suggest you educate the publisher of your data about the virtues of using only ISO 8601 formats when communicating date-time data textually.

The Z on the end is pronounced “Zulu”, and means an offset from UTC of zero hours-minutes-seconds. Another standard way to write an offset of zero is +00:00. The ISO 8601 standard permits omitting the COLON character, to get +0000 as seen in your Question.

I recommend always including the COLON character in an offset, as well as always including the leading padding zero when needed. While both are optional in the standard, I have seen multiple libraries and protocols that expect them.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154