This discussion is archived
2 Replies Latest reply: Jan 22, 2013 9:26 PM by 986291 RSS

Question on java.util.Date class.

986291 Newbie
Currently Being Moderated
The book that I am reading says, Date class represents a long int value since Jan 01 , 1970.


I created a Date class as follows


long st = 2000L;
d.setTime(st);
System.out.println("New time is ="+d.toString());

When the above code in run, it prints the date as follows.

New time is =Wed Dec 31 16:00:02 PST 1969

Any idea why the date printed in before 1970 ? I was expecting the date displayed to be some value greater than 01, Jan , 1970.
  • 1. Re: Question on java.util.Date class.
    rp0428 Guru
    Currently Being Moderated
    Welcome to the forum!
    >
    The book that I am reading says, Date class represents a long int value since Jan 01 , 1970.

    I created a Date class as follows


    long st = 2000L;
    d.setTime(st);
    System.out.println("New time is ="+d.toString());

    When the above code in run, it prints the date as follows.

    New time is =Wed Dec 31 16:00:02 PST 1969

    Any idea why the date printed in before 1970 ? I was expecting the date displayed to be some value greater than 01, Jan , 1970.
    >
    Because of the way that the setTime method is defined. What timezone are you using?

    http://docs.oracle.com/javase/1.5.0/docs/api/java/sql/Date.html#setTime(long)
    >
    setTime
    public void setTime(long date)Sets an existing Date object using the given milliseconds time value. If the given milliseconds value contains time information, the driver will set the time components to the time in the default time zone (the time zone of the Java virtual machine running the application) that corresponds to zero GMT.
  • 2. Re: Question on java.util.Date class.
    986291 Newbie
    Currently Being Moderated
    Thank You

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points