Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > java.sql.Date returning wrong date

Reply
Thread Tools

java.sql.Date returning wrong date

 
 
Steve Austin
Guest
Posts: n/a
 
      03-30-2007
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

 
Reply With Quote
 
 
 
 
Daniel Dyer
Guest
Posts: n/a
 
      03-30-2007
On Fri, 30 Mar 2007 16:28:48 +0100, Steve Austin <>
wrote:

> 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.

--
Daniel Dyer
http://www.uncommons.org
 
Reply With Quote
 
 
 
 
Chris Uppal
Guest
Posts: n/a
 
      03-30-2007
Steve Austin wrote:

> 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


 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      03-30-2007
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?)

--

 
Reply With Quote
 
Steve Austin
Guest
Posts: n/a
 
      03-30-2007
On Mar 30, 11:57 am, Eric Sosman <Eric.Sos...@sun.com> wrote:
> 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?)
>
> --
> Eric.Sos...@sun.com- Hide quoted text -
>
> - Show quoted text -


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.

 
Reply With Quote
 
Greg R. Broderick
Guest
Posts: n/a
 
      03-30-2007
"Steve Austin" <> wrote in news:1175271868.765004.183870
@d57g2000hsg.googlegroups.com:

> 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 gregb+

A. Top posters.
Q. What is the most annoying thing on Usenet?
---------------------------------------------------------------------
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      03-30-2007
Steve Austin wrote:
> 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.


Your wish is granted!

<http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html>

Those codes have been in the javadocs forever.

-- Lew
 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      04-05-2007

"Eric Sosman" <> wrote in message
news:1175270254.565324@news1nwk...
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Date, date date date.... Peter Grison Java 10 05-30-2004 01:20 PM
Date object's getDay returns wrong date for Feb in any leap year David Woodward Javascript 5 02-02-2004 08:22 PM
Given a date, how to find the beginning date and ending date of that week Matt ASP .Net 1 11-08-2003 09:14 PM
Given a date, how to find the beginning date and ending date of that week Matt C Programming 3 11-08-2003 09:07 PM
Given a date, how to find the beginning date and ending date of that week Matt C++ 2 11-08-2003 08:30 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57