Reputation: 67
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?
Upvotes: 0
Views: 2524
Reputation: 340090
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.
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
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.
Upvotes: 3