• Post Reply Bookmark Topic Watch Topic
  • New Topic

Converting String to SQL Date  RSS feed

 
Ed Moore
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having trouble converting a String to a SQL Date.

Here is the code:

This is the output:
Date as String; 10/21/2013 SqlDate: 2012-12-30
Date as String; 10/21/2013 SqlDate: 2012-12-30

Regardless of the date provided SQL date is returning 2012-12-30. What am I missing?
 
Steven Squeers
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure if it helps but I can tell you that ISO format for SQL date is YYYYMMDD (or maybe YYYY-MM-DD; can't remember):






 
Ed Moore
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is correct and what is being written to DB. 2012-12-30.

However, isn't dateFormat.parse(response.getReturnedStartDate()) converting my string to a Java Date? And if so, shouldn't I be able to then get the time of that Date and convert to SQl Date?
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Patterns are case sensitive, and there is a difference between "YYYY" (week year) and "yyyy" (year).
 
Ed Moore
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's it. Thank you.
 
Ahsan Bagwan
Ranch Hand
Posts: 254
1
Java MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code throws a run time exception. Strange! The description is,

java.lang.IllegalArgumentException: Illegal pattern character 'Y'

Works if you replace with lowercase 'y' (docs mention the behavior). Why 'Y' doesn't cause the program to suspend with an exception is beyond me.

Edit: Rob is right on; 'Y' stands for week year. And maybe it is only added in Java 7 (I think; correct me if I am wrong).
 
Ed Moore
Greenhorn
Posts: 5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using Netbeans and did not receive that Exception.

I created a separate class to play and the lower case y made the difference.


2013/10/07 Date as String;
Mon Oct 21 00:00:00 MST 2013 Start Date as String; 10/21/2013 SqlDate: 2013-10-21
Next Date as String; 10/25/2013 SqlDate: 2013-10-25
 
Ahsan Bagwan
Ranch Hand
Posts: 254
1
Java MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ed
I think you are using JDK 7? If so, then it is working properly. 'Y' is assigned as week year on that version.
 
Ed Moore
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I am using JDK 7. Does it change in 8?
 
Ahsan Bagwan
Ranch Hand
Posts: 254
1
Java MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know for sure. Skimming through JSR 310 myself after seeing a new Date and Time API about to be released in Java 8.
 
Steven Squeers
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, week year..I came across that recently in my studying for OCPJP7. It makes no difference except for those few days at the end of one year and at the beginning of another.

http://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html#week_year
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!