This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

How to fetch Date Field from excel sheet in Java  RSS feed

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dinesh Remje
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 544
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

# 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
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 21458
96
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Rob Spoor
Sheriff
Posts: 21458
96
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!