• Post Reply Bookmark Topic Watch Topic
  • New Topic

Convert string to date  RSS feed

 
Roopesh Rana
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 :

 
Wayan Saryada
Ranch Hand
Posts: 119
Hibernate Java Spring
  • 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 Java Spring
  • 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 Java Spring
  • 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:


 
Campbell Ritchie
Marshal
Posts: 56599
172
  • 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.
 
Tim Holloway
Saloon Keeper
Posts: 18800
74
Android Eclipse IDE 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!