• 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Convert string to date

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am trying to convert a string into date


Exception :

 
Ranch Hand
Posts: 119
Hibernate Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roopesh,

The exception happened because your string date is not valid according to your SimpleDateFormat pattern.
 
Roopesh Rana
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Wayan Saryada wrote:Hi Roopesh,

The exception happened because your string date is not valid according to your SimpleDateFormat pattern.





Exception :

 
Wayan Saryada
Ranch Hand
Posts: 119
Hibernate Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to use / import java.util.Date instead of java.sql.Date in your code.
 
Roopesh Rana
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Wayan Saryada wrote:You need to use / import java.util.Date instead of java.sql.Date in your code.



Roopesh Rana wrote:  I used java.util.Date as well as java.sql.Date also



Exception :

 
Wayan Saryada
Ranch Hand
Posts: 119
Hibernate Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry, I missed read that you modified your code

You might want to do the following if you are using Java 8 Date Time API:


 
Marshal
Posts: 70636
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stop using the Date class.
Wayan Saryada is right. Use LocalDate or similar; see the Java™ Tutorials. Since Java8 came out over 3½ years ago, you most probably can use LocalDate.

I think you have managed to find the wrong forum and this discussion would fit better in a more general forum, so I shall move you.
 
Saloon Keeper
Posts: 22649
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Short answer. Don't feed a date string that's yyyy-mm-dddd to a date formatter that's expecting yyyy/mm/dd.

As has been mentioned, there are more civilised classes available now for date/time handling, but even the older stuff works just fine if you use them properly.
 
When all four tires fall off your canoe, how many tiny ads does it take to build a doghouse?
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic