Conversion of date from one timezone to another

R

RAMK

hi folks,
I have a java Date object known to be in one timezone T1. Now
I would like to convert the Date object to another timezone T2. I have
java TimeZone objects corresponding to both T1 and T2.
I have gone through some of the related messages in the group but Iam
bit confused. Any lights on this, much appreciated.

Thanks
 
T

Thomas Weidenfeller

RAMK said:
hi folks,
I have a java Date object known to be in one timezone T1.

If your statement is correct, then something is wrong, unless your time
zone T1 equals GMT. Date internally stores the date/time information in
GMT only - as number of milliseconds since 1970-01-01 00:00 GMT.

A Date can print itself - often using the system's local timezone. But
still, it's internal data is in GMT. It is just converted for printing.
Now
I would like to convert the Date object to another timezone T2.

You should never convert a Date object. Everything else in Java assumes
that Date is based on GMT.

I have
java TimeZone objects corresponding to both T1 and T2.
I have gone through some of the related messages in the group but Iam
bit confused. Any lights on this, much appreciated.

What do you really want to do? In case you want to print(!) the Date
object, and the Date.toString() is not enough (it usually isn't), then
have a look at SimpleDateFormat. If you really want to hold the data
based on a different time zone, you better implement something by your
own and leave Date alone.

/Thomas
 
A

Anton Spaans

RAMK said:
hi folks,
I have a java Date object known to be in one timezone T1. Now
I would like to convert the Date object to another timezone T2. I have
java TimeZone objects corresponding to both T1 and T2.
I have gone through some of the related messages in the group but Iam
bit confused. Any lights on this, much appreciated.

Thanks

java.util.Date object are ALWAYS represented by the 'number of milliseconds
since 00:00 1-1-19770 GMT'. A Date object is a *timestamp* (a given time,
whereever you are on earth). You are interested in the time-of-day of a
given location on earth. A time-of-day needs the time-zone to represent that
location (and rules, such as daylight saving times, etc).

Use the java.text.SimpleDateFormat class (and the java.util.Calendar class)
to get the time-of-day from a timestamp (java.util.Date):

Code:
// sdf contains a Calendar object with the default timezone.
Date date = new Date();
String formatPattern = ....;
SimpleDateFormat sdf = new SimpleDateFormat(formatPattern);

TimeZone T1;
TimeZone T2;
....
....
// set the Calendar of sdf to timezone T1
sdf.setTimeZone(T1);
System.out.println(sdf.format(date));

// set the Calendar of sdf to timezone T2
sdf.setTimeZone(T2);
System.out.println(sdf.format(date));

// Use the 'calOfT2' instance-methods to get specific info
// about the time-of-day for date 'date' in timezone T2.
Calendar calOfT2 = sdf.getCalendar();
 
T

Tony Dahlman

RAMK said:
hi folks,
I have a java Date object known to be in one timezone T1. Now
I would like to convert the Date object to another timezone T2. I have
java TimeZone objects corresponding to both T1 and T2.
I have gone through some of the related messages in the group but Iam
bit confused. Any lights on this, much appreciated.

Thanks
Is this the kind of thing you want to do? That is, display current or some
other date/time in two (or more) different time zones?

http://pws.prserv.net/ad/programs/Programs.html#ExtDateFormat

HTH -- Tony Dahlman
 
Joined
Jan 31, 2012
Messages
1
Reaction score
0
In fact I'm facing a similar problem.
I don't know if this is exactly what the person who started this thread wanted to know.
Let's try to talk in a rigorous way.
A date object (basically a wrapper for a long timestamp) identifies a concrete moment in time and it's absolutely time-zone independent (a date object, strictly speaking, has no time zone). The reason to say it's GMT it's just anecdotic, because the epoch, on which timestamps are based, is defined as a GMT date (but the same epoch could be expressed in any other time zone and everything would work exactly the same).
So, yes, a Date object should never be "converted"........ assuming it was right in the first place.
Suppose you have a web application used by people from all around the globe.
When they write a date/time, they write it in their own timezone. It's not unusual that the framework you are developing on cannot determine the right timezone for the user and interprets the date/time entered by the user in its own default timezone, thus generating a wrong timestamp (a wrong Date object). Once this wrong timestamp arrives to your application, if your application knows the actual timezone of the user, you definitely want to fix the timestamp to represent the real moment in time the user meant when he wrote the date/time.
And this is where you get into trouble. You can (or may be you can't) bypass Date generation by the framework and "manually" parse the string with date/time info entered by the user and have a right Date object generated in the first place. But sounds more straightforward to let the framework do the wrong thing and then just centralize in one point of the application the "fix" of all received (or sent back to the user) Dates.
The question, for me, is: is there a simple, straightforward way to fix a wrong timestamp?
I tried to create a Calendar instance with the application default timezone, set the "wrong" Date, and then set the user's timezone, but the calendar does not touch the timestamp you set. Instead it will just show different values for the different fields when you query it.
So, the only way I find is a bit tedious:
- Create a Calendar object with default timezone (cal1).
- Set the wrong Date value
- Create a second Calendar object with user timezone (cal2).
- Get every date/time field from cal1 (except redundant ones). These should be exactly the ones the user entered, as they are being generated using the same timezone originally used to interpret the date/time string from the user.
- Set all these values in cal2. Calendar internal timestamp will be calculated this time using the right timezone.
- Get the fixed Date object from cal2.

I think this would work, but does anybody know about a more straightforward way to do it, or a library that already implemented this?

Thanks
 

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

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top