Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Date from ISO 8601 Zulu string to java.time.Instant in Java 8

I want to convert string date format into java.time.Instant

I am getting exception while parsing date.

 java.lang.IllegalArgumentException: Too many pattern letters: s

I am using below code for conversion first from String to date.

    String string = "2018-07-17T09:59:51.312Z";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYY-MM-DD'T'hh:mm:ss.sssZ", Locale.FRANCE);
    LocalDate date = LocalDate.parse(string, formatter);
    System.out.println(date);

I want to convert "timestamp": "2018-07-17T09:59:51.312Z" in format time in the ISO 8601 format YYYY-MM-DDThh:mm:ss.sssZ in the UTC timezone.

Checked Java string to date conversion, but not working.

like image 372
surendrapanday Avatar asked Aug 18 '18 16:08

surendrapanday


2 Answers

tl;dr

convert string date format into java.time.Instant

Skip the formatting pattern. Just parse.

Instant.parse( "2018-07-17T09:59:51.312Z" )

ISO 8601

Yes, you used incorrect formatting pattern as indicated in the first Answer.

But you needn't specify a formatting pattern at all. Your input string is in standard ISO 8601 format. The java.time classes use ISO 8601 formats by default when parsing/generating strings.

The Z on the end means UTC, and is pronounced “Zulu”.

Instant instant = Instant.parse( "2018-07-17T09:59:51.312Z" ) ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

  • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • Later versions of Android bundle implementations of the java.time classes.
    • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

like image 106
Basil Bourque Avatar answered Oct 14 '22 19:10

Basil Bourque


Don’t build your own formatter

Don’t struggle with writing your own format pattern string. Your string is in an ISO 8601 format that is built into java.time. To parse into a java.time.Instant (as your title says):

    String string = "2018-07-17T09:59:51.312Z";
    Instant inst = Instant.parse(string);
    System.out.println(inst);

Output:

2018-07-17T09:59:51.312Z

To parse into a LocalDate (as your code snippet seems to intend):

    LocalDate date = LocalDate.parse(string, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
    System.out.println(date);

2018-07-17

What went wrong in your code?

Format pattern letters are case sensitive (you can also see this from the tables in the first answer to the question you are linking to).

  • You used uppercase YYYY for year. Uppercase Y is for week year, only useful with a week number. Use either uuuu or lowercase yyyy for year.
  • Uppercase DD is for day of year. For day of month you need lowercase dd.
  • Lowercase hh is for hour with AM or PM from 01 through 12 and only useful with an AM/PM marker. You need uppercase HH for hour of day from 00 through 23.
  • Finally you correctly used lowercase ss for seconds, but also sss for fraction of second. For the latter you need uppercase SSS. This error is the reason for your error message: Since seconds only go up to 60 (including a leap second), three s as in sss does not make sense, and DateTimeFormatter objects to this. From the documentation:

    Up to two letters of 'd', 'H', 'h', 'K', 'k', 'm', and 's' can be specified.

Link: DateTimeFormatter documentation

like image 28
Ole V.V. Avatar answered Oct 14 '22 19:10

Ole V.V.