Getting date and time from different time zone issue

Hi

I have been to trying to get date and time from different time zone.

But some how it does not work, here is my code,

import java.util.*;

publicclass TestDateConversion

{

public TestDateConversion()

{

Calendar cEst = Calendar.getInstance();

Calendar cCst = Calendar.getInstance(TimeZone.getTimeZone("GMT"));

System.out.println ("cEst " + cEst.getTime() +" time zone " + cEst.getTimeZone().getID());

System.out.println ("cCST " + cCst.getTime()+" time zone " + cEst.getTimeZone().getID());

}

publicstaticvoid main(String args[])

{

new TestDateConversion();

}

}

This is the output

cEst Mon Oct 24 09:40:31 EDT 2005 time zone America/New_York

cCST Mon Oct 24 09:40:31 EDT 2005 time zone America/New_York

I also tried

System.setProperty("user.timezone", "GMT");

But with no luck, i am running windows XP with j2sdk1.4.2_06

Ashish

[1538 byte] By [kulkarniasha] at [2007-10-2 3:18:50]
# 1

An instant in time is the same instant all over the world, regarldess of time zone. How the instant in time is presented depends on the formatting. The default formatter uses the system time zone, which in your case is EDT. To format a time in a different time zone, use the DateFormat class.import java.text.*;

Date now = new Date();

DateFormat dfNormal = DateFormat.getDateTimeInstance();

DateFormat dfGMT = DateFormat.getDateTimeInstance();

dfGMT.setTimeZone(TimeZone.getTimeZone("GMT")); // format using GMT

print("Here: " + dfNormal.format(now));

print("There: " + dfGMT.format(now));

> System.out.println ("cCST " + cCst.getTime()+ " time zone " + cEst.getTimeZone().getID());

btw you're calling getTimeZone on cEst when you should be calling it on cCst, that's why you see the contradiction in the second line of the printout

jsalonena at 2007-7-15 21:46:17 > top of Java-index,Java Essentials,Java Programming...
# 2

Hello

In that case, i am having following problem and how do i fix it,

I have a web application on a server running in EST, there is applet in this web application and the client is in EST.

The server does some date calculation and send this information to the client. The client now does some more calculation and send them back to server. But now there is issue with this as i am expecting some other result.

The issue i traced was with date getting created on server with TimeZone as EST and at client where TimeZone is CST

So in JVM i added following paramter -Duser.timezone=EST and now it works fine

I wanted to replicate this behaviour in code, or to set time zone automatically when creating date instance. So i dont have to worry even if we move the server from EST to some other time zone, and dont have to do any setting in the code.

Ashish

kulkarniasha at 2007-7-15 21:46:17 > top of Java-index,Java Essentials,Java Programming...
# 3

> The server does some date calculation and send this

> information to the client. The client now does some

> more calculation and send them back to server. But

> now there is issue with this as i am expecting some

> other result.

>

What is the nature of these calculations, and how does the server pass the date to the client?

If you pass a string like 2005/24/10 22:21:05 then you'll certainly have problems, because 22:21 (the current time over here) in another time zone will be something completely different...

jsalonena at 2007-7-15 21:46:17 > top of Java-index,Java Essentials,Java Programming...
# 4

What i am doing in applet is plotting gant chart, so i convert date in number of pixcels, for example

if i have a date say 2005-10-10 and if the start date of gant chart is 2005-10-01, now if one day = 24 pixcels then i will plot this as 24X10 240 pixcels from left

i do some calculation on server and some on the client, and this causes issue when dates are created in different timezones.

Ashish

kulkarniasha at 2007-7-15 21:46:17 > top of Java-index,Java Essentials,Java Programming...
# 5

> i do some calculation on server and some on the

> client, and this causes issue when dates are created

> in different timezones.

>

how does the problem manifest itself?

bear in mind that the date is different in different parts of the world, e.g. where I am it's october 25 (19:15), in new zealand it's october 26 (05:11)... likewise 00:00 in EST is 23:00 CST on the previous day (I guess)

jsalonena at 2007-7-15 21:46:17 > top of Java-index,Java Essentials,Java Programming...