new java.util.Date(0)  
Author Message
Aaron Fude





PostPosted: 2004-6-3 2:20:00 Top

java-programmer, new java.util.Date(0) 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


 
Michael Borgwardt





PostPosted: 2004-6-3 2:28:00 Top

java-programmer >> new java.util.Date(0) 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.

 
Eric Sosman





PostPosted: 2004-6-3 2:58:00 Top

java-programmer >> new java.util.Date(0) 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));

--
email***@***.com

 
 
Roedy Green





PostPosted: 2004-6-3 3:13:00 Top

java-programmer >> new java.util.Date(0) On Wed, 2 Jun 2004 14:20:28 -0400, "Aaron Fude" <email***@***.com>
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.
 
 
Liz





PostPosted: 2004-6-3 4:09:00 Top

java-programmer >> new java.util.Date(0)
"Eric Sosman" <email***@***.com> wrote in message
news:email***@***.com...
> 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));
>
> --
> email***@***.com
>


 
 
Oscar kind





PostPosted: 2004-6-5 7:16:00 Top

java-programmer >> new java.util.Date(0) Aaron Fude <email***@***.com> 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