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.
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.
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
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();
}