Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Possible to treat time in milliseconds as a different time zone? (http://www.velocityreviews.com/forums/t960859-possible-to-treat-time-in-milliseconds-as-a-different-time-zone.html)

laredotornado@zipmail.com 05-21-2013 02:58 PM

Possible to treat time in milliseconds as a different time zone?
 
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

Arne Vajh°j 05-21-2013 03:23 PM

Re: Possible to treat time in milliseconds as a different time zone?
 
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



Eric Sosman 05-21-2013 03:31 PM

Re: Possible to treat time in milliseconds as a different time zone?
 
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

Stanimir Stamenkov 05-21-2013 08:09 PM

Re: Possible to treat time in milliseconds as a different time zone?
 
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_TIM EZONE));
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

Nigel Wade 05-22-2013 09:00 AM

Re: Possible to treat time in milliseconds as a different time zone?
 
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

Roedy Green 05-22-2013 10:03 AM

Re: Possible to treat time in milliseconds as a different time zone?
 
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)

Jukka Lahtinen 05-22-2013 10:08 AM

Re: Possible to treat time in milliseconds as a different time zone?
 
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

Daniele Futtorovic 05-22-2013 02:46 PM

Re: Possible to treat time in milliseconds as a different time zone?
 
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.

Robert Klemme 05-26-2013 12:23 PM

Re: Possible to treat time in milliseconds as a different time zone?
 
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/

Michal Kleczek 05-27-2013 09:45 AM

Re: Possible to treat time in milliseconds as a different time zone?
 
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


All times are GMT. The time now is 12:39 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.