Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Meeting Time - TimeZone

Status
Not open for further replies.

ld14088

MIS
Feb 27, 2005
1
US
Hi:
I am working on a program that given a specific time in one city, calculate the corresponding time in another city. Here is what I have so far:

String tz=TimeZone.getTimeZone("America/New_York").getDisplayName();
java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm z");
Date d=sdf.parse("2005-02-10 13:00 "+tz);
sdf.setTimeZone(TimeZone.getTimeZone("Europe/Paris"));
System.out.println("Time in Paris: "+sdf.format(d));

If I pass the start time zone and time for New York, I want to calculate the corresponding time for Paris. So, for 12:00pm NY, I would get 6pm in Paris in the winter time, but I should also get 6pm in the Summer time since both time zones adjust +1 hour. But, my code will display 7pm for Paris in June. Can someone provide help or suggestion to get the Daylight Saving Time correct? Thanks.
 
This should show you how to do what you wish :

Code:
		TimeZone tzNY = TimeZone.getTimeZone("America/New_York");
		TimeZone tzParis = TimeZone.getTimeZone("Europe/Paris");

		// Get the current time in this time zone
		long now = System.currentTimeMillis();

		// Get the two dates, given the time "now" plus the offsets for the
		// desired time zones
		java.util.Date dateNY = new java.util.Date(now +tzNY.getOffset(now));
		java.util.Date dateParis = new java.util.Date(now +tzParis.getOffset(now));

		System.out.println("Date in NY = " +dateNY);
		System.out.println("Date in Paris = " +dateParis);

You'll have to alter this so you can pass in your own date ...

--------------------------------------------------
Free Database Connection Pooling Software
 
Oh, daylight saving is handled automatically by the TimeZone object when you get the offsets ...

--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top