Date formatting problem

R

Rizwan

Hi All,

My task is to retrieve 2 dates from a table; manipulate them based on some
rules and then use them in a SELECT statement to get data from another
table. That SELECT statement is "SELECT sum(hours) FROM time_sheet WHERE
date_worked between :startDate and :endDate".


First I got those 2 dates from a table and store them in 2 java.util.Date
variables:

ResultSet rs = null;
....
java.util.Date startDate = rs.getDate("start_date");
java.util.Date endDate = rs.getDate("start_date");
....

When I print these variables they print in yyyy-MM-dd format:
startDate= 2005-01-17
endDate= 2005-01-30

Now i add 6 days in the endDate

Calendar aCalendar = Calendar.getInstance();
aCalendar.setTime( endDate );
aCalendar.add( Calendar.DATE, 6 ); // add days to the Calendar
endDate = aCalendar.getTime(); // convert from Calendar to
date

Now when I print endDate variable, it prints in different format:
endDate = Sun Jan 23 00:00:00 EST 2005


My question is why it is now printing in this format? I now cannot use them
in my SELECT statement becuase of this format. Can anybody help me please?

Thanks


Rizwan
 
L

Lee Fesperman

Rizwan said:
Hi All,

My task is to retrieve 2 dates from a table; manipulate them based on some
rules and then use them in a SELECT statement to get data from another
table. That SELECT statement is "SELECT sum(hours) FROM time_sheet WHERE
date_worked between :startDate and :endDate".

First I got those 2 dates from a table and store them in 2 java.util.Date
variables:

ResultSet rs = null;
...
java.util.Date startDate = rs.getDate("start_date");
java.util.Date endDate = rs.getDate("start_date");
...

You just accessed the same date twice.
When I print these variables they print in yyyy-MM-dd format:
startDate= 2005-01-17
endDate= 2005-01-30

That's impossible because they are the same date. You should cut & paste rather than
re-entering the code. This can make it very hard to diagnose your problem. However, it
doesn't in this case....
Now i add 6 days in the endDate

Calendar aCalendar = Calendar.getInstance();
aCalendar.setTime( endDate );
aCalendar.add( Calendar.DATE, 6 ); // add days to the Calendar
endDate = aCalendar.getTime(); // convert from Calendar to
date

Now when I print endDate variable, it prints in different format:
endDate = Sun Jan 23 00:00:00 EST 2005

My question is why it is now printing in this format? I now cannot use them
in my SELECT statement becuase of this format. Can anybody help me please?

Because rs.getDate() returns a java.sql.Date value. Storing it in a java.util.Date value
doesn't change the actual type. Later, you retrieve it as a java.util.Date value using
aCalendar.getTime(). Looks like java.sql.Date and java.util.Date format their dates
differently.

Either you can put endDate back into a java.sql.Date object at the end or you can use
java.text.SimpleDateFormat to get the format you need ... assuming you want the date in
string format for the original SELECT.
 
T

Tor Iver Wilhelmsen

Rizwan said:
My question is why it is now printing in this format?

Because the first Date is really a java.sql.Date which has a different
toString() than java.util.Date which is what the latter returns.
I now cannot use them > in my SELECT statement becuase of this >
format. Can anybody help me please?

java.text.SimpleDateFormat and the API docs.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,470
Messages
2,571,809
Members
48,797
Latest member
PeterSimpson
Top