1

How can I convert the following timestamp "2012-02-22T16:46:28.9870216+00:00" into a timestamp readable and processable by Java without losing any accuracy? what its data format?? for example about my goal, I receive 2 messages, read the timestamp in each message (as it written in my example above) compare between two timestamps to determine which is the old between them

by noting that the question below does not match what I need, it is different where they speak about timestamp format is different of 2012-02-22T16:46:28.9870216+00:00 which I did not know about it Converting ISO 8601-compliant String to java.util.Date

Sarru Uken
  • 83
  • 1
  • 4

1 Answers1

0

You can parse it with this format: DateTimeFormatter.ISO_OFFSET_DATE_TIME.

public static void main(String[] args) throws IOException {
    String str = "2012-02-22T16:46:28.9870216+00:00";
    DateTimeFormatter dtf = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
    System.out.println(dtf.parse(str));
}

If you run this code, you will see on the console:

{OffsetSeconds=0, InstantSeconds=1329929188},ISO resolved to 2012-02-22T16:46:28.987021600

Update:

Here is an updated response which shows how to convert the string to a LocalDateTime and allows date comparisons:

public static void main(String[] args) throws IOException {
    String str = "2012-02-22T16:46:28.9870216+00:00";
    DateTimeFormatter dtf = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
    LocalDateTime lds = LocalDateTime.parse(str, dtf);
    System.out.println(lds);
    String str2 = "2012-02-22T16:46:28.9970430+00:00";
    String str3 = "2012-02-22T16:46:29.0270266+00:00";
    LocalDateTime lds2 = LocalDateTime.parse(str2, dtf);
    LocalDateTime lds3 = LocalDateTime.parse(str3, dtf);
    System.out.println(lds2.compareTo(lds3));
    System.out.println(lds3.compareTo(lds2));
}

Update 2:

If you want to print out a date in this format you have to use java.time.OffsetDateTime, like so:

public static void main(String[] args) throws IOException {
    OffsetDateTime now = OffsetDateTime.now();
    DateTimeFormatter dtf = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
    System.out.println(dtf.format(now));
}

This prints out:

2018-03-18T16:46:46.715Z
gil.fernandes
  • 12,978
  • 5
  • 63
  • 76
  • only one question? if you can put in your code part about how to compare between two timestamps for example this "2012-02-22T16:46:28.9970430+00:00" and this "2012-02-22T16:46:29.0270266+00:00" – Sarru Uken Mar 18 '18 at 14:54
  • @SarruUken You can create a LocalDateTime object which you then compare. See updated response. – gil.fernandes Mar 18 '18 at 15:00
  • Mr. @gil.fernandes please, everything is ok now, but I can not figure out how to get the (current timestamp) using exactly the same timestamp format ? can you please help me in this matter? – Sarru Uken Mar 18 '18 at 16:39
  • @SarruUken Please check the second update of my answer. – gil.fernandes Mar 18 '18 at 16:49
  • you are great Mr. @gil.fernandes thank you very much from my heart – Sarru Uken Mar 18 '18 at 16:53