new java.util.Date(0)

A

Aaron Fude

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
 
M

Michael Borgwardt

Aaron said:
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.
 
E

Eric Sosman

Aaron said:
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));
 
R

Roedy Green

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

Liz

Eric Sosman said:
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;
 
O

Oscar kind

Aaron Fude said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top