unparseable date что за ошибка
Java: unparseable date exception
While trying to transform the date format I get an exception:unparseable date and don’t know how to fix this problem.
I am receiving a string which represents an event date and would like to display this date in different format in GUI.
What I was trying to do is the following:
is dummy. I would like to get a date string in the following format:
and the input String example is the following:
Does anyone know how to convert the example date (String) above into a String format dd.MM.yyyy HH:mm:ss?
Edit: I fixed the wrong input date format but still it doesn’t work. Above is the pasted method and below is the screen image from debugging session.
#Update I ran
and there is UTC String in the array. It’s a strange problem.
I did a dirty hack that works:
But still I would prefer to transform the original input without cutting timezone away.
This code is written for Android phone using JDK 1.6.
3 Answers 3
Update: Okay, I did a test:
This correctly prints:
I encountered this error working in Talend. I was able to store S3 CSV files created from Redshift without a problem. The error occurred when I was trying to load the same S3 CSV files into an Amazon RDS MySQL database. I tried the default timestamp Talend timestamp formats but they were throwing exception:unparseable date when loading into MySQL.
This from the accepted answer helped me solve this problem:
By the way, the «unparseable date» exception can here only be thrown by SimpleDateFormat#parse(). This means that the inputDate isn’t in the expected pattern «yyyy-MM-dd HH:mm:ss z». You’ll probably need to modify the pattern to match the inputDate’s actual pattern
The key to my solution was changing the Talend schema. Talend set the timestamp field to «date» so I changed it to «timestamp» then I inserted «yyyy-MM-dd HH:mm:ss z» into the format string column view a screenshot here talend schema
I had other issues with 12 hour and 24 hour timestamp translations until I added the «z» at the end of the timestamp string.
java.text.ParseException: Unparseable date
I am getting a parsing exception while I am trying the following code:
Exception in thread «main» java.text.ParseException: Unparseable date: «Sat Jun 01 12:53:10 IST 2013» at com.ibm.icu.text.DateFormat.parse(DateFormat.java:510)
Input: Sat Jun 01 12:53:10 IST 2013
Expected output: Jun 01,2013 12:53:10
9 Answers 9
Your pattern does not correspond to the input string at all. It is not surprising that it does not work. This would probably work better:
Then to print with your required format you need a second SimpleDateFormat:
Update your format to:
ISO 8601
Instead a format such as yours, use ISO 8601 standard formats for exchanging date-time values as text.
The java.time classes use the standard ISO 8601 formats by default when parsing/generating strings.
Proper time zone name
Your IST could mean Iceland Standard Time, India Standard Time, Ireland Standard Time, or others. The java.time classes are left to merely guessing, as there is no logical solution to this ambiguity.
java.time
The modern approach uses the java.time classes.
Define a formatting pattern to match your input strings.
About java.time
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
I am trying to parse a date, but I am oddly getting an exception.
java.text.ParseException: Unparseable date: «Wed, 09 Feb 2011 12:34:27» at java.text.DateFormat.parse(DateFormat.java:337) at DateTest.main(DateTest.java:17)
I have read the documentation and I think my pattern is correct. So I don’t understand.
3 Answers 3
It’s probably because of the default locale on your computer which is not english.
Details
The Question and other Answer both use outdated troublesome old date-time classes that are now legacy, supplanted by the java.time classes.
Using java.time
Specify a Locale to determine (a) the human language for translation of name of day, name of month, and such, and (b) the cultural norms deciding issues of abbreviation, capitalization, punctuation, separators, and such.
Time Zone
Both the Question and other Answer ignore the crucial issue of time zone.
The input string lacks a time zone or offset. We parsed as an LocalDateTime which is not a moment on the timeline, only a vague idea about possible moments. Like saying «Christmas begins at midnight on December 25, 2017», that has no meaning until you place it in the context of a particular time zone. Christmas comes much earlier in Auckland New Zealand than it does in Paris France, and much later still in Montréal Québec.
Converting
Best to avoid the troublesome old legacy date-time classes. But if you must interact with old code not yet updated to the java.time types, you can convert between the legacy classes and java.time. Look to new methods add to the old classes.
Going the other direction.
About java.time
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.
SimpleDateFormat «Unparseable date» Exception
I am trying to parse datetime string with SimpleDateFormat.parse() but I keep receiving Unparseable date exceptions.
Here is the date format I am trying to parse: 2011-10-06T12:00:00-08:00
Here is the code I am using:
Which returns this error: java.text.ParseException: Unparseable date: «2011-10-06T12:00:00-08:00»
As far as I know this is the correct way to use the SimpleDateFormat class but I’m not fluent in Java so I could be mistaken. Any one know what my issue is?
8 Answers 8
In Java 7 you can use «yyyy-MM-dd’T’HH:mm:ssX»
Some simple string manipulation should help you get rid of the colon.
After, create the formating for formataction desired. Ex: SimpleDateFormat fmt = new SimpleDateFormat («dd / MM / yyyy HH: mm: ss»);
and after make parse for date. Ex: Date date = dateParser.parse (dateFormat); and print of date formated.
Below, one complete example.
java.time
The modern approach uses the java.time classes that supplant the troublesome old legacy date-time classes.
You input string is in a format that complies with the ISO 8106 standard. The java.time classes use these standard formats by default when parsing/generating strings. So no need to specify a formatting pattern.
Parse as an OffsetDateTime because your input strings includes an offset-from-UTC but not a time zone.
Generate a string in your desired format. Let java.time automatically localize rather than hard-code formatting patterns.
When seralizing a date-time value as text, use the standard ISO 8601 formats rather than a localized format.
About java.time
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.
Java unparseable date
There is the following code:
And I get the following exception «Unparseable date: «2000-01-01T01:00:00Z» (at offset 4)». How can I fix it?
7 Answers 7
Wrong Parsing Pattern
You defined a formatting pattern that says you expect the input string to be hours and minutes. But your input string has much more, years, months, and so on.
java.time
As of Java 8 and later, the old java.util.Date/.Calendar and java.text.SimpleDateFormat have been supplanted by the new java.time package. Avoid the old classes whenever possible as they have proven to be confusing, troublesome, and flawed.
Your input string is using one of the standard date-time formats defined by ISO 8601. Fortunately, java.time uses that format by default.
An Instant is a moment on the timeline basically in UTC. You may adjust the value to a particular time zone (a ZoneId ), producing a ZonedDateTime object.
If you call toString on a ZonedDateTime, you will get a string like:
The ZonedDateTime class extends the ISO 8601 format by appending the name of the time zone in brackets.
Use this format instead:
The date in the exception is an xml schema dateTime. Note that simply creating
Will not work entirely as you might think because the Z indicates «UTC» timezone and the parser is by default initialized as local time.
Also note that the schema dateTime has a variable definition, it can (optionally) have millisecond precision (0 or more milliseconds) and the timezone (if something other than Z) is not compatible with the format of SimpleDateFormat.
In short: xml date times are tricky with the default libraries. I have written a custom solution for handling them in our environment but you could also look at the joda time library which I believe handles them well. Or you could wait for the next java version which will have a new date API.