java.text.ParseException: Unparseable date: "130625143100" - java

this is my code:
SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss");
Date date = sdf.parse("130625143100");
i have a long list of date strings, all in the same format as above one,
wierd thing is some success, some fail.
can any one help out with WHY this happens.
thanks a lot.

You said, some string parses well while some string fails. Put your code in trycatch block. Log the string for which it is failing. The string may not be as per the format you specified in SimpleDateFormat

SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss");
Date date = sdf.parse("130625143100");
mistake in doblue quote

You may need to validate your strings, for example if the value of month is greater than 12 then it will fail or for similar reasons.

Related

Getting original date from java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime()) and extracting Month

I am using Calendar.getInstance().getTime() to get current date and time in android, additionally following a response is saw on Stackoverflow I am using java.text.DateFormat.getDateTimeInstance().format() to get the string representation of the date I generated earlier and store it in a table. However how can I convert from the string representation back to the Calendar format it came from, and how can I extract the month from there? Please I have tried SimpleDateFormat and it asks me to place suppress warnings all over the place and brings errors. Please any help or better advice or guidance will be greatly appreciated, thxs.
This should give you an idea about how to work with calendar, how to format and parse a date:
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Date currentDate = calendar.getTime();
String formattedDate = sdf.format(currentDate);
Date reParsedDate = sdf.parse(formattedDate);
calendar.setTime(reParsedDate);
int month = calendar.get(Calendar.MONTH);
Nevertheless it would be better to keep your date object in some variable instead of reparsing it from the formatted string!

Java SimpleDateFormat ParseException pattern seems to match

I have used SimpleDateFormat to parse Strings into Dates many times in the past, but today I ran across an error that I cannot seem to see.
I am parsing a csv, and I have this:
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date date = dateFormat.parse(nextLine[0]);
I get this error
java.text.ParseException: Unparseable date: "2011-06-17 21:43:17.493"
It looks to me like the format matches the string when referencing the javadoc for SimpleDateFormat here:
http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
am I missing something with SimpleDateFormat?
Nothing is worng with your code. You just have invisible symbols at this line
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
You probably copied it from a PDF file or other source that inject that symbols into your text.
Try to copy it manually and it will be fine. It work for me after I done so.

Date format error with "2011-07-27T06:41:11+00:00"

I'm trying to format a time/date string:
String date = "2011-07-27T06:41:11+00:00";
DateFormat formatter = new SimpleDateFormat("yyyy MM-dd'T'HH:mm:ssz"); //2011-07-27T06:41:11+00:00
Date Sdate = formatter.parse(date.toString());
This is throwing the error
unable to parse newDate.
I don't understand why I'm getting this error, can someone explain?
The issue is with the TimeZone information. The ':' is an illegal character in the timezone string. See http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html#rfc822timezone and http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html#timezone
The following string is parseable
String date = "2011-07-27T06:41:11+0000";
Remove the ':' and your code will work.
SimpleDateFormat do not accept all ISO8601 date-time formats .
You can use DatatypeConverter.parseDateTime in JAXB .
something like
String date = "2011-07-27T06:41:11+00:00";
Date Sdate = DatatypeConverter.parseDateTime(date).getTime();
and please do try to follow the conventions (variable names should start with a lower case)
Try JodaTime. Java's built-in Date handling is not that good.

String to Date in a PreparedStatement

I am trying to use setDate() in a PreparedStatement, however the date that I have is in the format of 2008-07-31. The code is:
pstmt.setDate(f++, (Date) DateFormat.getDateInstance(DateFormat.DEFAULT).parse(value.substring(0, 10)));
However, it gives me the error:
Exception in thread "main" java.text.ParseException: Unparseable date: "2008-07-31"
Why is this?
If you have a very specific date, don't ask Java to use a default date format - set it yourself.
For example:
DateFormat parser = new SimpleDateFormat("yyyy-MM-dd");
Date date = parser.parse(value.substring(0, 10));
You should also potentially set the time zone of the parser... my guess is that UTC is the most appropriate time zone here.
Note that this has nothing to do with prepared statements as such - it's just date parsing.
(As an alternative to using DateFormat and SimpleDateFormat, you could use Joda Time which has a nicer API and thread-safe formatters/parsers. You can ask Joda Time to convert from its own types to Date values. Possibly overkill if you only need it for parsing here, but if you're doing anything else with dates, it's well worth looking into.)
You need make sure the default DateFormat is in yyyy-MM-dd format (usually it's a config in OS), or you can use SimpleDateFormat or java.sql.Date to parse date string.
java.util.Date d;
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd");
d = sdf.parse ("2008-07-31");
// or
d = java.sql.Date.valueOf ("2008-07-31");
or, you could just set parameter as String, if the underlying database driver support the VARCHAR/CHAR to DATE conversion.
DateFormat.DEFAULT points to MEDIUM format and MEDIUM format looks like Jan 12, 1952. So, you may have create a SimpleDateFormat object with the format you are using.
I think there is mismatch in the format of the date that you are providing as input and the format in which you have specified while formatting which is default in your case.
SimpleDateFormat parser = new SimpleDateFormay("yyyy-MM-dd");
Try using the same format for both the dates.
First convert String to Date and then set that to PreparedStatement. Check with below code.
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
Date convertedDate = dateFormat.parse(dateString);
I'd use
pstmt.setDate(f++,
new java.sql.Date(
new SimpleDateFormat("yyyy-MM-dd")
.parse(value.substring(0, 10))
.getTime()
)
);

Date Format - Java

I want to accept a Date format according to an ISO (can't remember which one)...
2009-09-17T13:03:00
How do I do this? I'm currently using a SimpleDateFormat but when I run my unit test against it, it fails.
DateFormat df = SimpleDateFormat("yyyy-MM-ddTHH:mm:ss");
Unit Test is passing it this string:
String test1 = "2009-09-17T13:07:01";
The SimpleDateFormat parameter should be "yyyy-MM-dd'T'HH:mm:ss".
Regards.
You need to enclose the T in single quotes
Your format is wrong. It should be something like this,
SimpleDateFormat isoFormat =
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
An easy Solution is to remove the "T" ;-)
...
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
Date date = df.parse("2009-09-17T13:07:01".replace("T",""))
Regards, Jan

Categories