• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

convert a String into Date with SimpleDateFormat

 
mark reusen
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
did you try using
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd")
 
mark reusen
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I tried also ("yyyy-MM-dd")
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 144
Fedora Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
mark reusen
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10429
133
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mike Zal
Ranch Hand
Posts: 144
Fedora Java Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic