Java SimpleDateFormat ParseException pattern seems to match - java

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.

Related

Convert timestamp to Epoch in java

I am receiving a timestamp like : 07/23/2019.08.45 from a system.
I need to convert this to Epoch.
I was not able to parse this format by using SimpleDateFormat.
I tried:
SimpleDateFormat SDF = new SimpleDateFormat("MM/dd/yyyy.HH:mm");
Date dateInstance =SDF.parse("07/23/2019.08.45");
but it didn't work
I also tried to split and parse the time and date but still got the unable to parse error
Can someone please help.
What would be the most efficient way to get this done.
You are not providing the correct date format:
SimpleDateFormat SDF = new SimpleDateFormat("MM/dd/yyyy.HH:mm");
Date dateInstance =SDF.parse("07/23/2019.08:45");
Notice : instead of .

GWT cannot parse timezones like BST ans EDT

I'm kinda new to GWT.
I have been trying to parse a date in the format "dd-MMM-yyyy HH:mm z" but I get an exception when the timezone is EDT or BST.
Doesn't GWT support these timezones while parsing? If so, is there any alternative way which I can use to parse the date using EDT?
Please help.
Code snippet:
DateTimeFormat dateParser = DateTimeFormat.getFormat("dd-MMM-yyyy HH:mm z");
String fomattedDate = dateParser.format(date,Timezone.createTimeZone(TimeZoneConstants.americaNewYork));
Date newDate = dateTimeParser.parse(formattedDate);
This line gives me exception.
After reading the docs for DateTimeFormat it said that it supports a lower number of timezones while parsing.
This code works for me:
TimeZoneConstants timeZoneConstants = GWT.create(TimeZoneConstants.class);
DateTimeFormat dateParser = DateTimeFormat.getFormat("dd-MMM-yyyy HH:mm Z");
String formattedDate = dateParser.format(date, TimeZone.createTimeZone(timeZoneConstants.americaNewYork()));
Date newDate = dateParser.parse(formattedDate);
I have created TimeZoneConstants via deferred binding and changed the format to dd-MMM-yyyy HH:mm Z (capital Z) - with z I did got an IllegalArgumentException indeed.
If you still get issues, try inspect deeply the parsing function to track down the exact problem.

Migrate to FastDateFOrmat instead of SimpleDateFormat

I have an app that was using SimpleDateFormat that we have since multithreaded and SimpleDateFormat is causing some issues.
I have read a bit about FastDateFormat but have never used it and would like to know how to do the following.
Currently I do something along the lines of :
String myDateString = "2015-08-10";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date myDate = sdf.parse(myDateString);
What is the equivalent to that using FastDateFormat?
I got as far as
FastDateFormat fdf = FastDateFormat.getInstance("yyyy-MM-dd");
But after that I dont see a clearcut way to get a Date instance returned.

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

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.

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()
)
);

Categories