Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #24145 > unrolled thread

Possible to treat time in milliseconds as a different time zone?

Started bylaredotornado@zipmail.com
First post2013-05-21 07:58 -0700
Last post2013-05-27 11:45 +0200
Articles 10 — 10 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Possible to treat time in milliseconds as a different time zone? laredotornado@zipmail.com - 2013-05-21 07:58 -0700
    Re: Possible to treat time in milliseconds as a different time zone? Arne Vajhøj <arne@vajhoej.dk> - 2013-05-21 11:23 -0400
    Re: Possible to treat time in milliseconds as a different time zone? Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-05-21 11:31 -0400
    Re: Possible to treat time in milliseconds as a different time zone? Stanimir Stamenkov <s7an10@netscape.net> - 2013-05-21 23:09 +0300
    Re: Possible to treat time in milliseconds as a different time zone? Nigel Wade <nmw@ion.le.ac.uk> - 2013-05-22 10:00 +0100
      Re: Possible to treat time in milliseconds as a different time zone? Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2013-05-22 13:08 +0300
        Re: Possible to treat time in milliseconds as a different time zone? Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2013-05-22 16:46 +0200
    Re: Possible to treat time in milliseconds as a different time zone? Roedy Green <see_website@mindprod.com.invalid> - 2013-05-22 03:03 -0700
    Re: Possible to treat time in milliseconds as a different time zone? Robert Klemme <shortcutter@googlemail.com> - 2013-05-26 14:23 +0200
    Re: Possible to treat time in milliseconds as a different time zone? Michal Kleczek <michal_wytnijto@kleczek.org> - 2013-05-27 11:45 +0200

#24145 — Possible to treat time in milliseconds as a different time zone?

Fromlaredotornado@zipmail.com
Date2013-05-21 07:58 -0700
SubjectPossible to treat time in milliseconds as a different time zone?
Message-ID<9aa4dbef-0212-4823-9b31-1a6d54ee772c@googlegroups.com>
Hi,

I'm using Java 6.  I'm trying to see if there's a simple way to convert a long varaible (the number of milliseconds since 1970) to a timezone other than GMT.  I have another time zone string, MY_TIMEZONE, which could be a timezone string ("GMT-5"), but I'm figuring out this doesn't work ...

    long timeInMs = 1368921600000;
    final Calendar cal = Calendar.getInstance();
    cal.setTimeZone(TimeZone.getTimeZone(MY_TIMEZONE));
    cal.setTimeInMillis(timeInMs);
    final java.util.Date dateObj = cal.getTime();
    System.out.println(dateObj.toString());

Can I parse the time zone string to get the number of hours difference and then just add that?  Grateful for any elegant solutions.  Thanks, - Dave

[toc] | [next] | [standalone]


#24147

FromArne Vajhøj <arne@vajhoej.dk>
Date2013-05-21 11:23 -0400
Message-ID<519b9164$0$32104$14726298@news.sunsite.dk>
In reply to#24145
On 5/21/2013 10:58 AM, laredotornado@zipmail.com wrote:
> I'm using Java 6.  I'm trying to see if there's a simple way to convert a long varaible (the number of milliseconds since 1970) to a timezone other than GMT.  I have another time zone string, MY_TIMEZONE, which could be a timezone string ("GMT-5"), but I'm figuring out this doesn't work ...
>
>      long timeInMs = 1368921600000;
>      final Calendar cal = Calendar.getInstance();
>      cal.setTimeZone(TimeZone.getTimeZone(MY_TIMEZONE));
>      cal.setTimeInMillis(timeInMs);
>      final java.util.Date dateObj = cal.getTime();
>      System.out.println(dateObj.toString());
>
> Can I parse the time zone string to get the number of hours difference and then just add that?  Grateful for any elegant solutions.

The idea sounds wrong to me.

In Java time stored in a long is supposed to be in UTC. And there are
various classes to convert to and from time components based on time
zones.

Inventing a ms since 1970 local time concept would create massive
confusion in the code base.

Arne

[toc] | [prev] | [next] | [standalone]


#24149

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2013-05-21 11:31 -0400
Message-ID<kng3oi$knp$1@dont-email.me>
In reply to#24145
On 5/21/2013 10:58 AM, laredotornado@zipmail.com wrote:
> Hi,
>
> I'm using Java 6.  I'm trying to see if there's a simple way to convert a long varaible (the number of milliseconds since 1970) to a timezone other than GMT.  I have another time zone string, MY_TIMEZONE, which could be a timezone string ("GMT-5"), but I'm figuring out this doesn't work ...
>
>      long timeInMs = 1368921600000;
>      final Calendar cal = Calendar.getInstance();
>      cal.setTimeZone(TimeZone.getTimeZone(MY_TIMEZONE));
>      cal.setTimeInMillis(timeInMs);
>      final java.util.Date dateObj = cal.getTime();
>      System.out.println(dateObj.toString());

     What do you mean by "doesn't work?"  (Besides "doesn't compile,"
that is.)  What were you hoping to get, and what did you see, and
in what way did it disappoint you?

> Can I parse the time zone string to get the number of hours difference and then just add that?  Grateful for any elegant solutions.  Thanks, - Dave

     The getTimeZone() method has already parsed the string (or
tried to), and you can extract its results from the TimeZone object.

-- 
Eric Sosman
esosman@comcast-dot-net.invalid

[toc] | [prev] | [next] | [standalone]


#24156

FromStanimir Stamenkov <s7an10@netscape.net>
Date2013-05-21 23:09 +0300
Message-ID<kngk18$pha$1@dont-email.me>
In reply to#24145
Tue, 21 May 2013 07:58:28 -0700 (PDT), /laredotornado@zipmail.com/:

> I'm using Java 6.  I'm trying to see if there's a simple way to
> convert a long varaible (the number of milliseconds since 1970)
> to a timezone other than GMT.  I have another time zone string,
> MY_TIMEZONE, which could be a timezone string ("GMT-5"), but I'm
> figuring out this doesn't work ...
>
>      long timeInMs = 1368921600000;
>      final Calendar cal = Calendar.getInstance();
>      cal.setTimeZone(TimeZone.getTimeZone(MY_TIMEZONE));
>      cal.setTimeInMillis(timeInMs);
>      final java.util.Date dateObj = cal.getTime();
>      System.out.println(dateObj.toString());
>
> Can I parse the time zone string to get the number of hours
> difference and then just add that?  Grateful for any elegant
> solutions.  Thanks, - Dave

What do you expect when you say "convert"?

As others have pointed out the java.util.Date type is a simple 
wrapper for a timestamp value which is always in UTC.  If you 
initialize a java.util.Calendar instance and set its timeZone - 
you've already converted it in a way.  When you query the Calendar 
fields HOUR_OF_DAY, MINUTE, etc. you've got your conversion.  You 
may use these values to initialize your custom type of time object, 
also.

If you want to _format_ a time/timestamp in a text representation 
using specific time-zone, instead, you may use a java.util.DateFormat:

     final String MY_TIMEZONE = "JST";
     long timeInMs = 1368921600000L;
     DateFormat dateFormat = DateFormat.getDateTimeInstance();
     System.out.println(dateFormat.format(new Date(timeInMs)));
     dateFormat.setTimeZone(TimeZone.getTimeZone(MY_TIMEZONE));
     System.out.println(dateFormat.format(new Date(timeInMs)));

Note, your example code does really nothing to the Calendar.time 
value, even if you change the Calendar's timeZone in between:

     long timeInMs = 1368921600000L;
     Calendar cal = Calendar.getInstance();
     cal.setTimeZone(TimeZone.getTimeZone(MY_TIMEZONE));
     cal.setTimeInMillis(timeInMs);
     java.util.Date dateObj = cal.getTime();
     cal.setTimeZone(TimeZone.getTimeZone(MY_TIMEZONE2));
     java.util.Date dateObj2 = cal.getTime();
     System.out.println(dateObj.getTime() == dateObj2.getTime());

-- 
Stanimir

[toc] | [prev] | [next] | [standalone]


#24163

FromNigel Wade <nmw@ion.le.ac.uk>
Date2013-05-22 10:00 +0100
Message-ID<b03fohFaoriU1@mid.individual.net>
In reply to#24145
On 21/05/13 15:58, laredotornado@zipmail.com wrote:
> Hi,
>
> I'm using Java 6.  I'm trying to see if there's a simple way to convert a long varaible (the number of milliseconds since 1970) to a timezone other than GMT.  I have another time zone string, MY_TIMEZONE, which could be a timezone string ("GMT-5"), but I'm figuring out this doesn't work ...
>
>      long timeInMs = 1368921600000;
>      final Calendar cal = Calendar.getInstance();
>      cal.setTimeZone(TimeZone.getTimeZone(MY_TIMEZONE));
>      cal.setTimeInMillis(timeInMs);
>      final java.util.Date dateObj = cal.getTime();
>      System.out.println(dateObj.toString());
>
> Can I parse the time zone string to get the number of hours difference and then just add that?  Grateful for any elegant solutions.  Thanks, - Dave
>

Timezone is an artificial concept.

In Java the Date object represents a particular instant in time, and is the number of milliseconds since 1970. This is
independent of timezone. You can't "convert" it to another timezone. This principle underpins the entire Date, Calendar
and TimeZone concept.

In order to view a Date in "human" terms, i.e. wallclock time, timezones are used. This converts an instant in time
represented by the Date object into a wallclock time in a particular timezone.

For example, the time in UK now is 09:50 BST. This represents a particular instant in time and has an associated Date
value. In New York the wallclock time is 04:50 EDT. However, the Date for the UK time is the same as the Date for the
New York time since they are the same instant in time, only the local representation changes.

So, your code won't work since all timezones will have the same Date for a particular instant in time. Your objective
appears to be backwards. What are you actually trying to achieve?

-- 
Nigel Wade

[toc] | [prev] | [next] | [standalone]


#24166

FromJukka Lahtinen <jtfjdehf@hotmail.com.invalid>
Date2013-05-22 13:08 +0300
Message-ID<lvvc6bnx7h.fsf@saunalahti.fi>
In reply to#24163
Nigel Wade <nmw@ion.le.ac.uk> writes:

> Timezone is an artificial concept.
> In Java the Date object represents a particular instant in time, and is
> the number of milliseconds since 1970. This is

"1970" is as artificial as timezone.
The beginning of the year is just as timezone-dependent as any other
point of time after that.
The Date object represents milliseconds after beginning of year 1970 in
a specific timezone. 

-- 
Jukka Lahtinen

[toc] | [prev] | [next] | [standalone]


#24169

FromDaniele Futtorovic <da.futt.news@laposte-dot-net.invalid>
Date2013-05-22 16:46 +0200
Message-ID<knilgn$joi$1@dont-email.me>
In reply to#24166
On 22/05/2013 12:08, Jukka Lahtinen allegedly wrote:
> Nigel Wade <nmw@ion.le.ac.uk> writes:
> 
>> Timezone is an artificial concept.
>> In Java the Date object represents a particular instant in time, and is
>> the number of milliseconds since 1970. This is
> 
> "1970" is as artificial as timezone.
> The beginning of the year is just as timezone-dependent as any other
> point of time after that.

Nigel was being concise since he assumed he was talking to an informed
public. The complete formulation would have been: "the number of
milliseconds since midnight, January 1st, 1970 *UTC*". So it does indeed
represent a specific instant in time.

> The Date object represents milliseconds after beginning of year 1970 in
> a specific timezone. 

No. A Date object can represent any date as understood by humans. For
its *internal representation*, it uses the number of milliseconds since
midnight, January 1st, 1970 UTC, which it then converts to a calendar
day and timezone-specific time -- according to the settings of your
Calendar object and things like leap seconds, etc. -- for representation
purposes.

But to the API, the number of milliseconds always means the same thing.
You can change its meaning, but the API won't understand it.

-- 
DF.

[toc] | [prev] | [next] | [standalone]


#24164

FromRoedy Green <see_website@mindprod.com.invalid>
Date2013-05-22 03:03 -0700
Message-ID<lv5pp85rj058b0ff2nsdt5ee80pehvuho6@4ax.com>
In reply to#24145
On Tue, 21 May 2013 07:58:28 -0700 (PDT), laredotornado@zipmail.com
wrote, quoted or indirectly quoted someone who said :

>I'm using Java 6.  I'm trying to see if there's a simple way to convert a l=
>ong varaible (the number of milliseconds since 1970) to a timezone other th=
>an GMT.  I have another time zone string, MY_TIMEZONE, which could be a tim=
>ezone string ("GMT-5"), but I'm figuring out this doesn't work ...

see http://mindprod.com/jgloss/calendar.html
for various conversion recipes.
-- 
Roedy Green Canadian Mind Products http://mindprod.com
Technological possibilities are irresistible to man. 
If man can go to the moon, he will. 
If he can control the climate, he will. 
 ~ John von Neumann (born: 1903-12-28 died: 1957-02-08 at age: 53)

[toc] | [prev] | [next] | [standalone]


#24182

FromRobert Klemme <shortcutter@googlemail.com>
Date2013-05-26 14:23 +0200
Message-ID<b0ed5tFmpoiU1@mid.individual.net>
In reply to#24145
On 21.05.2013 16:58, laredotornado@zipmail.com wrote:

> I'm using Java 6.  I'm trying to see if there's a simple way to
> convert a long varaible (the number of milliseconds since 1970) to a
> timezone other than GMT.

As others have said already that is not necessary.  You need to 
distinguish two concepts:

1. point in time: this is represented by a number of milliseconds since 
a particular point in time in history.

2. representation of a point in time for various audiences (language, 
time zone).

Ideally internally to an application you use concept number 1 _all_ the 
time and make use of concept 2 when appropriate (e.g. when presenting a 
point in time to the user or writing it into a logfile).

> Can I parse the time zone string to get the number of hours
> difference and then just add that?  Grateful for any elegant
> solutions.

You can - but it's the wrong approach.

Cheers

	robert


-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

[toc] | [prev] | [next] | [standalone]


#24186

FromMichal Kleczek <michal_wytnijto@kleczek.org>
Date2013-05-27 11:45 +0200
Message-ID<knv9v8$nik$3@speranza.aioe.org>
In reply to#24145
On 2013-05-21 16:58, laredotornado@zipmail.com wrote:
>
> Can I parse the time zone string to get the number of hours difference and then just add that?

That's tricky since time difference between timezones is actually time 
dependent because of DST and other requlatory changes.

--
Michal

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web