This question already has answers here:
Converting ISO 8601-compliant String to java.util.Date
(31 answers)
ISO 8601 String to Date/Time object in Android
(5 answers)
Closed 4 years ago.
I'm trying to parse 1980-02-22T00:00:00Z into "java.util.Date" using
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SS'Z'").parse(stringdate)
But I got error
caused by: java.text.ParseException: Unparseable date.
How to parse String like this into Date to get time in milliseconds? I've tried to use SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SS'Z'") and SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
Just replace:
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SS'Z'")
by:
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
If you can use java8, you can use LocalDateTime class,
you can do below:
As per suggestions below, I have corrected my code to parse the date.
String text = "1980-02-22T12:10:02Z";
LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.parse(text),ZoneOffset.UTC);
System.out.println(dateTime);
Result:
1980-02-22T12:10:02
Related
This question already has answers here:
How to validate the DateTime string format "2018-01-22T18:23:00.000Z" in Java?
(2 answers)
Closed 2 years ago.
How to parse "2020-05-22T12:51:20.732111Z" to Instant in Java?
I used:
LocalDateTime.parse(
startTime, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US))
.atZone(ZoneId.of("America/Toronto"))
.toInstant()
but with error:
Exception in thread "main" java.time.format.DateTimeParseException: Text '2020-05-22T12:51:20.732111Z' could not be parsed at index 24
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.LocalDateTime.parse(LocalDateTime.java:492)
at Instant.Main.main(Main.java:54)
Use Instant#parse(String):
Instant i = Instant.parse("2020-05-22T12:51:20.732111Z");
And if you want to convert it to your timezone:
ZonedDateTime z = i.atZone(ZoneId.of("America/Toronto"));
Printing it yields:
2020-05-22T08:51:20.732111-04:00[America/Toronto]
This question already has answers here:
display Java.util.Date in a specific format
(11 answers)
want current date and time in "dd/MM/yyyy HH:mm:ss.SS" format
(11 answers)
Closed 2 years ago.
Im getting the following error when I try to convert the following string. Id like the Date to be in the format yyyy-MM-dd'T'HH:mm:ss:SSS but instead the Date seems to be coming out as Sun Mar 01 23:00:01 GMT 2020
String FULL_ISO_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS";
SimpleDateFormat formatter = new SimpleDateFormat(FULL_ISO_DATE_FORMAT);
Date from = formatter.parse("2020-03-01T23:00:01.000");
Error
feign.FeignException: status 400 reading Controller#searchController(Date,Date,Integer,String); content:
{"status":"fail","data":{"errors":[{"type":"IllegalArgumentException","description":"Invalid value Sun Mar 01 23:00:01 GMT 2020 for filter from. Field needs to be in format: yyyy-MM-dd'T'HH:mm:ss.SSS"}]}]}
Any help would be appreciated. I need to use the Date object as the constructor Im querying is using the Date object. Ideally I'd like to use LocalDateTime but I cant.
Use the LocalDateTime from java-8 date-time API and stop using legacy Date classes
String FULL_ISO_DATE_FORMAT = DateTimeFormatter. ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
LocalDateTime dateTime = LocalDateTime.now();
dateTime.format(FULL_ISO_DATE_FORMAT);
Please don't use the old classes Date and SimpleDateFormat. Use the new java.time api that is much more robust and better designed.
You can do the same thing as follows:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
LocalDateTime date = LocalDateTime.parse("2020-03-01T23:00:01.000", formatter);
Keep in mind that you can convert it to Date for compatibility like so:
Date legacyDate = Date.from(date.atZone(ZoneId.systemDefault()).toInstant());
This question already has answers here:
Is java.time failing to parse fraction-of-second?
(3 answers)
Closed 3 years ago.
So I am trying to parse:
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
TemporalAccessor parse = dateTimeFormatter.parse("20180521073438514");
And I receive the following error:
"Text '20180521073438514' could not be parsed at index 0"
But when I try to add sth that will separate "ss" and "SSS" it works:
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss.SSS");
TemporalAccessor parse = dateTimeFormatter.parse("20180521073438.514");
Am I missing some kind of delimiter to separate "ss" and "SSS"?
This is a bug in Java 8 which was fixed in Java 9. Have a look at the official bug report https://bugs.openjdk.java.net/browse/JDK-8031085
This question already has an answer here:
Gson: java.text.ParseException: Unparseable date: "2018-04-09T09:00:00+02:00"
(1 answer)
Closed 4 years ago.
I have a date in the following format:
2017-04-09T11:15:39.200+03:00
I used the following format string:
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
But i am getting an exception"
java.text.ParseException: Unparseable date: "2017-04-09T11:15:39.200+03:00"
Thanks
What's wrong is that you didn't read the documentation: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
You'd see that the pattern to use is X:
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
And you also didn't check in stackoverflow before posting: in the same tags you used (https://stackoverflow.com/questions/tagged/simpledateformat), there's another question - a very recent one, asked today - with basically the same problem (using 'Z' inside quotes):
Gson: java.text.ParseException: Unparseable date: "2018-04-09T09:00:00+02:00"
The API docs for what you say you want shows this as the for-matter for your output wish.
"yyyy-MM-dd'T'HH:mm:ss.SSSZ"
https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html
This question already has answers here:
How do I change date time format in Android?
(11 answers)
Closed 6 years ago.
I receive DATETIME from mysql database in a format like this 2016-10-12 18:52:00. Is there an easy way to print it in a readable like 12 October 2016 6:52 pm in java or android?
You can do things like:
SimpleDateFormat simpleDate = new SimpleDateFormat("MM/DD/YYYY HH:mm:ss");
Date date = new Date();
String S = simpleDate.format(date); //This will give you like 11/20/2016 08:45:02
Now use this form and get the month name programatically according to your need.