aspose file tools*
The moose likes Beginning Java and the fly likes  convert a String into Date with SimpleDateFormat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark " convert a String into Date with SimpleDateFormat" Watch " convert a String into Date with SimpleDateFormat" New topic
Author

convert a String into Date with SimpleDateFormat

mark reusen
Greenhorn

Joined: Oct 20, 2010
Posts: 22
I try to convert a String into Date with SimpleDateFormat by code below;

(gebDatum is formatted like "1980-01-03")

But it doesn't work

The reason is that I want to store the gebDatum in an MySQL database and it is defined as a Date in the database..... (gebDatum is coming from a form)

Can somebody tell me what is going wrong?
joy b chakravarty
Ranch Hand

Joined: May 16, 2011
Posts: 62

did you try using
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd")


Cheers, Joy [SCJP 1.4, SCBCD 5.0]
get high on alcohol, algorithm or both
mark reusen
Greenhorn

Joined: Oct 20, 2010
Posts: 22
Yes, I tried also ("yyyy-MM-dd")
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4366
    
    8

Well, that should work. Exactly what is happening - e.g. do you get an error message, or an incorrect answer? And have you confirmed that is the valud of gebDatum by printing it out?
mark reusen
Greenhorn

Joined: Oct 20, 2010
Posts: 22
I printed it in the log and the result after the parse is: "Tue Jun 25 00:00:00 CET 1963", and the gebDatum is: "1963-06-25"
Mike Zal
Ranch Hand

Joined: May 04, 2011
Posts: 144

mark reusen wrote:I printed it in the log and the result after the parse is: "Tue Jun 25 00:00:00 CET 1963", and the gebDatum is: "1963-06-25"


Last I checked, "1963-06-25" is June 26 of the year 1963. This shows you are converting to a date properly so why do you say that it is not working?


OCJP6, OCWCD5
mark reusen
Greenhorn

Joined: Oct 20, 2010
Posts: 22
The result I want is a Date object, so I can use it for updating the Date in the database.
The only thing what I want to happen is converting a String format to a Date format, both look like "1978-02-18".

The fault I receive by updating the Date in the database is like below:
exception java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date

and that is because of the unexpected format after parse.......
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5216
    
  12

And are you using some kind of ORM to map an object to your relational database, like Hibernate? Or why do you exactly need a java.sql.Date?

If you have look at the SimpleDateFormat API you'll notice that its parse-method will return a Date object. So that's certainly not the unexpected format after parsing. Furthermore is the java.sql.Date a subclass of the Date class, so you can use its constructor and the getTime-method to create an instance of the desired type.


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Mike Zal
Ranch Hand

Joined: May 04, 2011
Posts: 144

If you look at the java.sql.Date javadoc, it has a constructor that takes a long as its constructor. The java.util.Date class has a method that returns a long. It should not be hard to convert the two.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

mark reusen wrote:The fault I receive by updating the Date in the database is like below:
exception java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date

and that is because of the unexpected format after parse.......

No, that means the parsing from String to java.util.Date went successfully, but when you insert this in the database, for example using a PreparedStatement, it expects a java.sql.Date object and not a java.util.Date object. (Unfortunately the date and time APIs in Java's standard library are not great, so this is one of those problems you get...).

You have to create a java.sql.Date object out of your java.util.Date object to be able to use it to insert it into the database.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: convert a String into Date with SimpleDateFormat