• 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
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

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
Spring MySQL Database 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 :

 
I Wayan Saryada
Ranch Hand
Posts: 119
Spring MySQL Database 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 :

 
I Wayan Saryada
Ranch Hand
Posts: 119
Spring MySQL Database 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: 80865
505
  • 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: 28745
211
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic