• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

convert a String into Date with SimpleDateFormat

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 62
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I tried also ("yyyy-MM-dd")
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"
 
Ranch Hand
Posts: 144
Oracle Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.......
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Oracle Fedora Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
You get good luck from rubbing the belly of a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic