new java.util.Date(0)

Discussion in 'Java' started by Aaron Fude, Jun 2, 2004.

  1. Aaron Fude

    Aaron Fude Guest

    Hi,

    I'm sure this is some kind of Locale issue and is probably documented in the
    spec, but I looked there and there are many things documented there. It
    would be nice if the experts in this ng could explain the right way to think
    about it.

    System.out.println(new Date(0));

    yields

    Wed Dec 31 19:00:00 EST 1969

    I guess this is the time in NY (which is what my computer is set to) when in
    London it was 1/1/1970 00:00am. How can I use Date more generically?

    For example, I have code which returns historical temprature. If I ask it to
    return the temprature as of "1/1/1970" it will convert the string to the
    Date (which is Date(0)) and then return the temperature as of 12/31/1969. I
    could artificially add 5 hours, but then it won't work in CA. I could add 8
    hours but then it won't work in Moscow. I could add 1 day, but then it won't
    work in London.

    Thanks!

    Aaron Fude
     
    Aaron Fude, Jun 2, 2004
    #1
    1. Advertising

  2. Aaron Fude wrote:

    > For example, I have code which returns historical temprature. If I ask it to
    > return the temprature as of "1/1/1970" it will convert the string to the
    > Date (which is Date(0))


    Only in GMT. Timezones apply when converting from String to Date as well
    as in the opposite direction.
     
    Michael Borgwardt, Jun 2, 2004
    #2
    1. Advertising

  3. Aaron Fude

    Eric Sosman Guest

    Aaron Fude wrote:
    > Hi,
    >
    > I'm sure this is some kind of Locale issue and is probably documented in the
    > spec, but I looked there and there are many things documented there. It
    > would be nice if the experts in this ng could explain the right way to think
    > about it.
    >
    > System.out.println(new Date(0));
    >
    > yields
    >
    > Wed Dec 31 19:00:00 EST 1969
    >
    > I guess this is the time in NY (which is what my computer is set to) when in
    > London it was 1/1/1970 00:00am. How can I use Date more generically?


    The Date(long) constructor interprets its argument as
    seconds since 1970-01-01 00:00:00 UTC, but the toString()
    method produces a representation in the local time zone.
    (The accuracy of the representation depends on the host
    system's ability to support time zones, of course.) If
    you want to produce a representation in a possibly non-local
    time zone, use java.text.DateFormat and set its time zone
    explicitly, e.g.:

    Date d = new Date(0);
    System.out.println("toString: " + d);
    DateFormat df = DateFormat.getDateTimeInstance();
    System.out.println("Default format: " + df.format(d));
    df.setTimeZone(TimeZone.getTimeZone("UTC"));
    System.out.println("Near the Bow Bells: " + df.format(d));

    --
     
    Eric Sosman, Jun 2, 2004
    #3
  4. Aaron Fude

    Roedy Green Guest

    On Wed, 2 Jun 2004 14:20:28 -0400, "Aaron Fude" <>
    wrote or quoted :

    >For example, I have code which returns historical temprature. If I ask it to
    >return the temprature as of "1/1/1970" it will convert the string to the
    >Date (which is Date(0)) and then return the temperature as of 12/31/1969. I
    >could artificially add 5 hours, but then it won't work in CA. I could add 8
    >hours but then it won't work in Moscow. I could add 1 day, but then it won't
    >work in London.


    you want pure date, not a timestamp. See
    http://mindprod.com/products.html#BIGDATE.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Jun 2, 2004
    #4
  5. Aaron Fude

    Liz Guest

    "Eric Sosman" <> wrote in message
    news:...
    > Aaron Fude wrote:
    > > Hi,
    > >
    > > I'm sure this is some kind of Locale issue and is probably documented in

    the
    > > spec, but I looked there and there are many things documented there. It
    > > would be nice if the experts in this ng could explain the right way to

    think
    > > about it.
    > >
    > > System.out.println(new Date(0));
    > >
    > > yields
    > >
    > > Wed Dec 31 19:00:00 EST 1969
    > >
    > > I guess this is the time in NY (which is what my computer is set to)

    when in
    > > London it was 1/1/1970 00:00am. How can I use Date more generically?

    >
    > The Date(long) constructor interprets its argument as
    > seconds since 1970-01-01 00:00:00 UTC, but the toString()


    So does it implicitly convert 0 to 0L;

    > method produces a representation in the local time zone.
    > (The accuracy of the representation depends on the host
    > system's ability to support time zones, of course.) If
    > you want to produce a representation in a possibly non-local
    > time zone, use java.text.DateFormat and set its time zone
    > explicitly, e.g.:
    >
    > Date d = new Date(0);
    > System.out.println("toString: " + d);
    > DateFormat df = DateFormat.getDateTimeInstance();
    > System.out.println("Default format: " + df.format(d));
    > df.setTimeZone(TimeZone.getTimeZone("UTC"));
    > System.out.println("Near the Bow Bells: " + df.format(d));
    >
    > --
    >
    >
     
    Liz, Jun 2, 2004
    #5
  6. Aaron Fude

    Oscar kind Guest

    Aaron Fude <> wrote:
    > I'm sure this is some kind of Locale issue and is probably documented in the
    > spec, but I looked there and there are many things documented there. It
    > would be nice if the experts in this ng could explain the right way to think
    > about it.
    >
    > System.out.println(new Date(0));
    >
    > yields
    >
    > Wed Dec 31 19:00:00 EST 1969
    >
    > I guess this is the time in NY (which is what my computer is set to) when in
    > London it was 1/1/1970 00:00am. How can I use Date more generically?


    Use a Calendar object. Then you can set the timezone.


    > For example, I have code which returns historical temprature. If I ask it to

    [...]

    Note that the only non-abstract subclass of Calendar, GregorianCalendar,
    supports historically correct dates as far back as 4 AD.


    Oscar

    --
    Oscar Kind http://home.hccnet.nl/okind/
    Software Developer for contact information, see website

    PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2
     
    Oscar kind, Jun 5, 2004
    #6
    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. mgalaty
    Replies:
    0
    Views:
    2,728
    mgalaty
    Sep 10, 2004
  2. Replies:
    5
    Views:
    98,629
    vongeuca
    May 4, 2011
  3. Replies:
    5
    Views:
    5,604
  4. grz01
    Replies:
    3
    Views:
    2,351
    Arne Vajhøj
    Sep 18, 2009
  5. laredotornado
    Replies:
    16
    Views:
    7,874
    Lothar Kimmeringer
    Oct 27, 2009
Loading...

Share This Page