0

I'm trying to get Milliseconds since Epoch from ISO_DATE_TIME and i have been reading so many article.

My starting point is this: 2020-10-06T11:51:45.964+01:00

My ending point is a long that consider milliseconds. (https://currentmillis.com/)

I'm using a message coming from Kafka into Flink, and the time stamp is in the payload of the message.

This is my code:

public void setKafkaTime(String kafkaTime) {
    DateTimeFormatter sdr = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXXX");
    LocalDateTime MydateTime = LocalDateTime.parse(kafkaTime,sdr);
    NOT WORKING -> Date date = Date.from(MydateTime.toInstant(ZoneOffset.UTC));
    Long tn = date.getTime();
    this.time = tn;
}

There is so much about the topic and i tried many ways, starting from the Java doc:

https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_OFFSET_DATE_TIME But mine is not ISO-8601 standard as i have second and milliseconds.

i have been using some important tips from previous questions like this one, where i found the pattern Java string to date conversion and i got this nice table. enter image description here

To use .getTime() I need a java.util.Date to convert LocalDateTime to Date I got in trouble with an error asking me for a Java.sql.Date.

enter image description here

I have been reading about the difference between the two, and from what I understood Java.sql doesn't consider the time, so I denfinetly should not use it.

java.util.Date vs java.sql.Date

I'm confident about the top two lines of the code, for the rest I'm struggling. I really can't get how to obtain a long. Any help would be really appreciated.

Thanks

SimAzz
  • 138
  • 2
  • 14
  • 1
    Don’t use `Date`, it’s poorly designed, long outdated and a detour. Don’t use `LocalDateTime` as it cannot define a point in time and hence no milliseconds count from the epoch. You also don’t need to use any formatter. `OffsetDateTime.parse("2020-10-06T11:51:45.964+01:00").toInstant().toEpochMilli()` yields 1601981505964. – Ole V.V. Nov 04 '20 at 15:34
  • And yes, indeed your format is [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601). Seconds and fraction of second are optional in that format. – Ole V.V. Nov 04 '20 at 15:36
  • 1
    Many thanks, one line and it is all done! – SimAzz Nov 04 '20 at 19:01
  • SimAzz - Do not stop here...whenever you get some time, go read answers by @OleV.V. and you will learn about date-time much faster than you can do from a tutorial or a textbook. – Arvind Kumar Avinash Nov 06 '20 at 18:42
  • 1
    @Arvind Kumar Avinash - I will! thanks for the suggestion! s – SimAzz Nov 09 '20 at 17:06

1 Answers1

2

Try this. I used a zoned time and then converted that to an instant to get the milliseconds.

public  void setKafkaTime(String kafkaTime) {
    DateTimeFormatter sdr = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSzzz");
    ZonedDateTime MydateTime = ZonedDateTime.parse(kafkaTime,sdr);
    this.time = MydateTime.toInstant().toEpochMilli();
}
WJS
  • 36,363
  • 4
  • 24
  • 39