Pankaj
Pankaj

Reputation: 54

How do i get this Hex string converted to Date in java

I am trying to convert hex string "07e4070e04032b" to date and below is my code:

String hexmillis1 = "07e4070e04032b";
long convertedMillis1 = Long.decode("0x" + hexmillis1);
Instant instant1 = Instant.ofEpochMilli(convertedMillis1);
LocalDateTime localDateTime1 = LocalDateTime.ofInstant(instant1, ZoneId.systemDefault());

System.out.println(localDateTime1.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));

Output: +72352-01-29T13:17:02.571

Expected output is a date and time earlier today, so 2020-07-14 or perhaps 2020-07-13.

I have tried few other ways also but it does not seem to give correct date.

The hex string is coming from a SNMP trap. Not sure of how it was exactly encoded.

Upvotes: -1

Views: 529

Answers (1)

Frighi
Frighi

Reputation: 466

As you can see here the conversion is right.

What is wrong there is the hex value:

07e4070e04032b == 2221043788022571

Upvotes: 1

Related Questions