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

L

laredotornado

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
 
A

Arne Vajhøj

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
 
E

Eric Sosman

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

Stanimir Stamenkov

Tue, 21 May 2013 07:58:28 -0700 (PDT), /[email protected]/:
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());
 
N

Nigel Wade

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?
 
R

Roedy Green

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)
 
J

Jukka Lahtinen

Nigel Wade said:
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.
 
D

Daniele Futtorovic

"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.
 
R

Robert Klemme

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
 
M

Michal Kleczek

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.
 

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
474,056
Messages
2,570,440
Members
47,101
Latest member
DoloresHol

Latest Threads

Top