| Author |
How to fetch Date Field from excel sheet in Java
|
Dinesh Remje
Ranch Hand
Joined: Jan 26, 2010
Posts: 55
|
|
I am writing a program where i am fetching the records from an excel sheet and after getting those records i am trying to update it in database.
Now i am able to fetch the records but i am not able to set the date field in database.
In excel sheet the date is in date field format. So how can i fetch the date field from excel sheets.
|
 |
Dinesh Remje
Ranch Hand
Joined: Jan 26, 2010
Posts: 55
|
|
|
i am able to get the contents from the excel sheets with the help of getContents() method. But for inserting into database i want to get the date field in date format itself so how can i do that.
|
 |
Soumyajit Hazra
Ranch Hand
Joined: Jun 26, 2007
Posts: 136
|
|
|
If you are getting the content from the excel sheet as String then you can use java SimpleDateFormat class to format it according to your db format.
|
Java Programmer | SCJP 1.5 | SCWCD 1.4
|
 |
Dinesh Remje
Ranch Hand
Joined: Jan 26, 2010
Posts: 55
|
|
Ya i am able to do so. But the problem is i am getting time also with it. And i just want the date. So how can i ignore time and just take the date value.
This is what i have done
And this is what i get output for printing strdate
The error shows that i am not able to parse the date.
So what should i do in this case.
|
 |
amit punekar
Ranch Hand
Joined: May 14, 2004
Posts: 488
|
|
Hello,
If you are using Apache POI for accessing the excel sheet then you can use getDateValue for a Cell. This is more better approach instead of reading it as a string value and then converting it.
Regards,
Amit
|
 |
Soumyajit Hazra
Ranch Hand
Joined: Jun 26, 2007
Posts: 136
|
|
|
The date format what you are getting is "dd mm yyyy hh:MM:ss" not "yyyy-mm-dd" so you are getting the exception.
|
 |
Dinesh Remje
Ranch Hand
Joined: Jan 26, 2010
Posts: 55
|
|
I am usin jxl approach.
But in excel sheet i have set that field as "YYYY-MM-DD" format. So how i am getting the time here.
Shall i set that field to text format in excel sheet and see the changes???
|
 |
Soumyajit Hazra
Ranch Hand
Joined: Jun 26, 2007
Posts: 136
|
|
# java.text.ParseException: Unparseable date: "06 05 2011 12:00:00"
# at java.text.DateFormat.parse(DateFormat.java:337)
# at summerinterndates.Main.contentReading(Main.java:143)
# at summerinterndates.Main.init(Main.java:51)
# at summerinterndates.Main.main(Main.java:290)
From the exception we can see that "06 05 2011 12:00:00" is the string you are trying to format and which is not yyyy-mm-dd. You can use something like this
and you will get "2010-05-06" as output
|
 |
Dinesh Remje
Ranch Hand
Joined: Jan 26, 2010
Posts: 55
|
|
Thanks Soumyajit Hazra,
What i have done is format the date field in excel sheet as a text field. I was successfully able to get the date in format i wanted.
But now there is an other problem which i am facing right now.
the line which is marked bold, i am getting an error there. Where my netbeans specify me to do "stmt.setDate(1, (java.sql.Date) startdate);" but again in gives an exception while updating the table that "Error: java.util.Date cannot be cast to java.sql.Date" so what should be done in this case.
|
 |
Soumyajit Hazra
Ranch Hand
Joined: Jun 26, 2007
Posts: 136
|
|
|
What is the type of "startdate"? Remember SimpleDateFormat returns java.util.Date and if you want to use setDate() you require java.sql.Date. So convert the util date object to sql date object. Check the Date API for the related conversion help.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Dinesh Remje wrote:I am usin jxl approach.
JXL also has proper support for dates. Cell has a sub interface DateCell which has methods to get both the Date object and the DateFormat used. So just cast the current Cell to DateCell and use its getDate() method.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
Also, Dinesh, I've modified your last post to decrease the indentation. That way the forum doesn't need horizontal scroll bars. Next time please keep your indentation a bit in check when posting code. We like proper indentation, but not if each line has an indentation of several tabs.
|
 |
 |
|
|
subject: How to fetch Date Field from excel sheet in Java
|
|
|