-1

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.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Pankaj
  • 54
  • 1
  • 7
  • Do you what the other way ? How the hex value has been obtained ? – azro Jul 14 '20 at 14:40
  • What is the meaning of the hex string? I.e. how is the time encoded in it? – Henry Jul 14 '20 at 14:49
  • @azro, @@henry - it is comming from a snmp trap. Not sure of how it is exactly converted. – Pankaj Jul 14 '20 at 14:53
  • Thanks for your reply. i found my solution at : https://stackoverflow.com/questions/9700037/snmp-eventtime-in-human-readable-format-in-java – Pankaj Jul 14 '20 at 16:57
  • For your next question about code that gives a surprising result: Please state what result you had expected. Then we can much better help you how to obtain that result. – Ole V.V. Jul 14 '20 at 18:44
  • I took this occasion for writing [a new answer for you here](https://stackoverflow.com/a/62902927/5772882). – Ole V.V. Jul 14 '20 at 20:02

1 Answers1

1

As you can see here the conversion is right.

What is wrong there is the hex value:

07e4070e04032b == 2221043788022571

Frighi
  • 475
  • 4
  • 17
  • Wrong. SNMP trap date-time strings don’t work this way. See the linked original question for the correct answers. Or [this](https://www.webnms.com/snmp/help/snmpapi/snmpv3/using_mibs_in_applns/tcs_dateandtime.html). – Ole V.V. Jul 14 '20 at 20:03
  • @OleV.V. as far I answered, I didn't know about data comes from SNMP trap, I've just help guy debugging his problem confirming the conversion performed was right and the problem was in the hex source. – Frighi Jul 15 '20 at 07:01