3

I am storing my 2 Java date types as Date and Time for a MySQL database table. I am using the SimepleDateFormat("YYYY-MM-dd") to store the date in my database and it shows up as the correct date when i go to select it. However when i try to parse it back into a util.Date and create a new Event Object, it shows up as 30/12/2012 instead of 31/05/2013 as it is in the database. The time, when parsed into a util.Date and formatted prints out correctly. I am not sure why the Date is printing the wrong date, but the time is printing the correct time.

Database

+--------+--------------+-----------+
+ EVENT1 +  2013-05-31  +  02:30:00 +
+--------+--------------+-----------+
+ EVENT2 +  2013-05-31  +  01:00:00 +
+--------+--------------+-----------+

Prints:

Event1
30/12/2012
02:30
Event2
30/12/2012
01:00
5
  • 1
    this could be due to mismatch between data types between DB and Java.what is your column data type in MySQL.. if it is date then use rs.getDate() instaed of rs.getString().. Commented May 31, 2013 at 9:54
  • getDate returns a java.sql.Date which already extends java.util.Date Commented May 31, 2013 at 9:59
  • let me answer this question...pls see my answer Commented May 31, 2013 at 10:04
  • @Pavan Kumar K, i don't see your question. but i gave your comment an upvote. Commented May 31, 2013 at 10:05
  • please see my answer for this question... that should work out for you Commented May 31, 2013 at 10:06

5 Answers 5

4

It should be yyyy-MM-dd with lower case Ys. See here for what the capital Y means...

Y returns 2012 while y returns 2011 in SimpleDateFormat

Sign up to request clarification or add additional context in comments.

2 Comments

This solved it for some reason. I changed dateFormat to yyyy-MM-dd and it returned the correct Date (31/05/2013). Its strange because I used YYYY-MM-dd to INSERT the date into the database and it stored it as 31/05/2013 as well.....:\.
@user1352609 You can't expect development language (Java) and your DB to use the same patterns. YYYY is working with your DB but hasn't the same meaning than in Java
1

Your pattern is wrong. (mm != MM, yyyy != YYYY ...)

Take a look at http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Comments

0

try

String testDate = "2007-11-02T14:46:03";
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date date = formatter.parse(testDate);

But better way to store in database is to use timestamp instead of storing date and time separately

Comments

0

The proper method is rs.getDate(int). Take a look at http://docs.oracle.com/javase/6/docs/api/java/sql/ResultSet.html#getDate(int)

With that method you get a java.sql.Date and if you want to transform it to a java.util.Date take a look at this Converting java.sql.Date to java.util.Date

You can even do this

Date date =  rs.getTimestamp(2);

By the way, is better to have your date object independent on the format you want to use to show it.

Comments

-1

try this...

SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd");   
Date date =  dateFormat.parse(rs.getDate(2).toString());

1 Comment

-1, this makes no sense, you already have a Date (a java.sql.Date which is a subclass of java.util.Date)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.