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.
convert string date format into java.time.Instant
Skip the formatting pattern. Just parse.
Instant.parse( "2018-07-17T09:59:51.312Z" )
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" ) ;
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?
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.
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
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).
YYYY
for year. Uppercase Y
is for week year, only useful with a week number. Use either uuuu
or lowercase yyyy
for year.DD
is for day of year. For day of month you need lowercase dd
.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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With