java.sql.Date returning wrong date

Discussion in 'Java' started by Steve Austin, Mar 30, 2007.

  1. Steve Austin

    Steve Austin Guest

    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
     
    Steve Austin, Mar 30, 2007
    #1
    1. Advertising

  2. Steve Austin

    Daniel Dyer Guest

    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
     
    Daniel Dyer, Mar 30, 2007
    #2
    1. Advertising

  3. Steve Austin

    Chris Uppal Guest

    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
     
    Chris Uppal, Mar 30, 2007
    #3
  4. Steve Austin

    Eric Sosman Guest

    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 Sosman, Mar 30, 2007
    #4
  5. Steve Austin

    Steve Austin Guest

    On Mar 30, 11:57 am, Eric Sosman <> 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?)
    >
    > --
    > - 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.
     
    Steve Austin, Mar 30, 2007
    #5
  6. "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

    A. Top posters.
    Q. What is the most annoying thing on Usenet?
    ---------------------------------------------------------------------
     
    Greg R. Broderick, Mar 30, 2007
    #6
  7. Steve Austin

    Lew Guest

    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
     
    Lew, Mar 31, 2007
    #7
  8. Steve Austin

    Oliver Wong Guest

    "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
     
    Oliver Wong, Apr 5, 2007
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,285
    Michael Borgwardt
    May 30, 2004
  2. Replies:
    5
    Views:
    98,645
    vongeuca
    May 4, 2011
  3. Lew
    Replies:
    0
    Views:
    470
  4. Replies:
    5
    Views:
    5,610
  5. grz01
    Replies:
    3
    Views:
    2,353
    Arne Vajhøj
    Sep 18, 2009
Loading...

Share This Page