java.sql.Date returning wrong date

S

Steve Austin

Hi can you assist me in finding the root cause of this problem. I am
converting a simple date of "2007-04-20" to SQL date. However for
some reason the conversion is not working. It seems to be defaulting
too 2007-01-20. Hmm.. perhaps its the getTime() not returning the
correct number of milliseconds since January 1, 1970. Not sure.
Anyway would appreciate a secound pair of eyes on this one. Thanks



import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;


//The code


public class Testing {
public static void main(String[] args) {

try {
SimpleDateFormat formater = new SimpleDateFormat("yyyy-mm-
dd");
String inDateClaimed = "2007-04-20";
java.util.Date parsedDate = formater.parse(inDateClaimed);
System.out.println("inDateClaimed=" +inDateClaimed);
Date result = new java.sql.Date(parsedDate.getTime());
System.out.println("result=" +result);



} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

}
}

//The Output


inDateClaimed=2007-04-20
result=2007-01-20
 
D

Daniel Dyer

Hi can you assist me in finding the root cause of this problem. I am
converting a simple date of "2007-04-20" to SQL date. However for
some reason the conversion is not working. It seems to be defaulting
too 2007-01-20. Hmm.. perhaps its the getTime() not returning the
correct number of milliseconds since January 1, 1970. Not sure.
Anyway would appreciate a secound pair of eyes on this one. Thanks

....

SimpleDateFormat formater = new SimpleDateFormat("yyyy-mm-dd");

Years, minutes, days?

Dan.
 
C

Chris Uppal

Steve said:
Hi can you assist me in finding the root cause of this problem. I am
converting a simple date of "2007-04-20" to SQL date. However for
some reason the conversion is not working. It seems to be defaulting
too 2007-01-20. Hmm.. perhaps its the getTime() not returning the
correct number of milliseconds since January 1, 1970. Not sure.
SimpleDateFormat formater = new SimpleDateFormat("yyyy-mm-dd");

'm' means "minutes".

If you change the example so that it prints the value of parsedDate, then the
problem becomes clear.

-- chris
 
E

Eric Sosman

Steve Austin wrote On 03/30/07 11:28,:
Hi can you assist me in finding the root cause of this problem. I am
converting a simple date of "2007-04-20" to SQL date. However for
some reason the conversion is not working. It seems to be defaulting
too 2007-01-20. Hmm.. perhaps its the getTime() not returning the
correct number of milliseconds since January 1, 1970. Not sure.
Anyway would appreciate a secound pair of eyes on this one. Thanks



import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;


//The code


public class Testing {
public static void main(String[] args) {

try {
SimpleDateFormat formater = new SimpleDateFormat("yyyy-mm-
dd");

Bzzzt! What do you imagine "mm" means to SimpleDateFormat?
To put it another way, what do you suppose you'd see if you also
displayed the time of day from the converted Date?

(Despite the identity of my employer, I cannot think of any
good reason for this bizarre behavior. It has at one time or
another tripped up everybody and his Uncle Charlie, and will keep
doing so for the forseeable future. That's "lost productivity,"
exactly the sort of thing Java was supposed to reduce. Could we
have had an easily-remembered mnemonic device like "Capital letters
for the big time units, lower-case for the small?" No, that would
make too much sense. Can *anyone* suggest a way to remember the
date format string codes short of tattooing them to the inside of
one's eyelids?)
 
S

Steve Austin

Steve Austin wrote On 03/30/07 11:28,:




Hi can you assist me in finding the root cause of this problem. I am
converting a simple date of "2007-04-20" to SQL date. However for
some reason the conversion is not working. It seems to be defaulting
too 2007-01-20. Hmm.. perhaps its the getTime() not returning the
correct number of milliseconds since January 1, 1970. Not sure.
Anyway would appreciate a secound pair of eyes on this one. Thanks
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
//The code
public class Testing {
public static void main(String[] args) {
try {
SimpleDateFormat formater = new SimpleDateFormat("yyyy-mm-
dd");

Bzzzt! What do you imagine "mm" means to SimpleDateFormat?
To put it another way, what do you suppose you'd see if you also
displayed the time of day from the converted Date?

(Despite the identity of my employer, I cannot think of any
good reason for this bizarre behavior. It has at one time or
another tripped up everybody and his Uncle Charlie, and will keep
doing so for the forseeable future. That's "lost productivity,"
exactly the sort of thing Java was supposed to reduce. Could we
have had an easily-remembered mnemonic device like "Capital letters
for the big time units, lower-case for the small?" No, that would
make too much sense. Can *anyone* suggest a way to remember the
date format string codes short of tattooing them to the inside of
one's eyelids?)

Thanks for catching this, I have not looked at the date and time
pattern strings in a while. I will refresh my memory with all the
patterns. Thanks again!. In response to your question "Can *anyone*
suggest a way to remember the date format". Perhaps if Sun puts all
the possible pattern letters in the javadoc in SimpleDateFormat.java.
I use RAD 6 which displays the SimpleDateFormat doc in a tooltip when
one is constructing an object of type SimpleDateFormat.
 
G

Greg R. Broderick

In response to your question "Can *anyone*
suggest a way to remember the date format". Perhaps if Sun puts all
the possible pattern letters in the javadoc in SimpleDateFormat.java.

They're there, at least in my copy of the JDK 1.5 javadocs, the listing of
all pattern letters is given in the class javadoc for SimpleDateFormat.

Cheers!

--
---------------------------------------------------------------------
Greg R. Broderick (e-mail address removed)

A. Top posters.
Q. What is the most annoying thing on Usenet?
---------------------------------------------------------------------
 
O

Oliver Wong

Eric Sosman said:
Steve Austin wrote On 03/30/07 11:28,:
public class Testing {
public static void main(String[] args) {

try {
SimpleDateFormat formater = new SimpleDateFormat("yyyy-mm-
dd");

Bzzzt! What do you imagine "mm" means to SimpleDateFormat?
To put it another way, what do you suppose you'd see if you also
displayed the time of day from the converted Date?

(Despite the identity of my employer, I cannot think of any
good reason for this bizarre behavior. It has at one time or
another tripped up everybody and his Uncle Charlie, and will keep
doing so for the forseeable future. That's "lost productivity,"
exactly the sort of thing Java was supposed to reduce. Could we
have had an easily-remembered mnemonic device like "Capital letters
for the big time units, lower-case for the small?" No, that would
make too much sense. Can *anyone* suggest a way to remember the
date format string codes short of tattooing them to the inside of
one's eyelids?)

How about a checkstyle/findbugs/lint/whatever plugin/module/whatever
that flags any occurrence of passing "yyyy-mm-dd" to SimpleDataFormat(),
as it's very rare to want to include minutes between years and days. More
generally, any sequence of 'm' between 'y' and 'd' with no other
alphabetic characters (e.g. ' ', '-' and '/' are not alphabetic
characters, and so don't count) in between is a danger sign.

- Oliver
 

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

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top