Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > new java.util.Date(0)

Reply
Thread Tools

new java.util.Date(0)

 
 
Aaron Fude
Guest
Posts: n/a
 
      06-02-2004
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


 
Reply With Quote
 
 
 
 
Michael Borgwardt
Guest
Posts: n/a
 
      06-02-2004
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.

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      06-02-2004
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));

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      06-02-2004
On Wed, 2 Jun 2004 14:20:28 -0400, "Aaron Fude" <(E-Mail Removed)>
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.
 
Reply With Quote
 
Liz
Guest
Posts: n/a
 
      06-02-2004

"Eric Sosman" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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));
>
> --
> (E-Mail Removed)
>



 
Reply With Quote
 
Oscar kind
Guest
Posts: n/a
 
      06-04-2004
Aaron Fude <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Javascript new-new-new-new-newbee weblinkunlimited@gmail.com Javascript 2 03-11-2008 01:15 AM
New computer, New OS, New Wireless Problem :-\ =?Utf-8?B?RGFu?= Wireless Networking 3 07-31-2005 02:11 PM
[Firefox] Use New Tab instead of New Window? paul j Firefox 7 04-07-2005 09:40 PM
Why can not register a new .net passport or a new hotmail account Alick Lv MCSD 1 01-04-2004 06:12 PM



Advertisments