• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Convert string to date

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 74342
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 24499
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic