Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calendar Conversions

 
Buck_Webb
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to make a little program that will print out the times of cities around the world. To do this I am creating different Calendar objects. Here is what I have so far:
//This will get the time in Raleigh, NC (Local Time)
Calendar raleighTime = Calendar.getInstance();
Date d = raleighTime.getTime();
SimpleDateFormat formatter = new SimpleDateFormat ("hh:mm a zzz");
String dateString = formatter.format(d);
System.out.print(dateString);
//This will get the time in Paris, France
Calendar parisTime =
Calendar.getInstance(tz.getTimeZone("Europe/Paris"));
Date parisDate = parisTime.getTime();
String parisDateString = formatter.format(parisDate);
System.out.print(parisDateString);
Maybe I just don't understand what I am doing :-) but I would think that because I put in the TimeZone object of Europe/Paris that when I use the getTime() method Java will return the local time adjusted for Paris. No, nothing is that simple. It prints out the local time (in North Carolina). I can't figure out how to get the time in Paris (I know that I could add 6 hours, but this would not adjust for Daylight Savings Time). Can anyone lend a helping hand??
Thanks,
Buck
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Buck_Webb",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please log in with a new name which meets the requirements.
Thanks.
 
Kaspar Dahlqvist
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!
I haven't got a clue here... It feels like I've tried every single way (except the right one, I guess), and it FAILS EVERY TIME.
THERE IS NO DARN WAY TO CHANGE THE TIMEZONE!!!
Help, please!!!
/Kaspar
 
Eric Edwards
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Buck_Webb:
I am looking at a copy of "just JAVA 2", Fourth Edition.
On page 384 it shows a table for the Java.util API. In the table is listed the area of Calendar,TimeZone,SimpleTimeZone.
On page 404 is the following code:
TimeZone z_ect = TimeZone.getTmeZone("ECT");
GregorianCalendar g2 = new GregorianCalendar(z_ect);
g2.set(89, 10, 9, 19, 0); // Berlin Wall //Down Nov 9 1989 7pm
g2.set(89, Calendar.NOVEMBER, 9, 19,0); //better
I don't know if this is what you need, but give it a try.
Good Luck!
Eric
[This message has been edited by Eric Edwards (edited July 13, 2001).]
 
Kaspar Dahlqvist
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
This code manually alters time... I was searching for something that expressed NOW in terms of a different timezone, eg + or - a couple of hours...
/Kaspar
 
Buck Webb
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frank Carver, sorry about the underscore. I guess I'm just used to typing them.
Thank you Eric for the code posting, but I am trying to do something a little different. Every country has a different day when they go on daylight savings time. While Paris is six hours ahead of North Carolina (at this time of the year), if I just add six hours to the local time, I will have to change this code about four times a year. I would like to do this for about 15 countries, so that quickly grows to a lot of mods.
I know that there is a way to alter the date for daylight savings time using a Calendar. It has fields such as DST_OFFSET (daylight savings time offset in milliseconds). I just can figure out how to do it. I just want to get the local time, set the TimeZone (such as "Europe/Paris"), and then ask for the time. I can't get this to work. I think that I am missing something key.
Thank you everyone for your time!!!
Buck
 
Roy Tock
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the API definition of the TimeZone class.
What follows is part of the API definition. I wouldn't normally copy this here, but I've got the API definition loaded on my hard drive, so I don't know a public link.
---
You can also get a TimeZone using getTimeZone along with a time zone ID. For instance, the time zone ID for the U.S. Pacific Time zone is "America/Los_Angeles". So, you can get a U.S. Pacific Time TimeZone object with:
TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");

You can use getAvailableIDs method to iterate through all the supported time zone IDs. You can then choose a supported ID to get a TimeZone. If the time zone you want is not represented by one of the supported IDs, then you can create a custom time zone ID with the following syntax:
GMT[+|-]hh[[:]mm]

For example, you might specify GMT+14:00 as a custom time zone ID. The TimeZone that is returned when you specify a custom time zone ID does not include daylight savings time.
For compatibility with JDK 1.1.x, some other three-letter time zone IDs (such as "PST", "CTT", "AST") are also supported. However, their use is deprecated because the same abbreviation is often used for multiple time zones (for example, "CST" could be U.S. "Central Standard Time" and "China Standard Time"), and the Java platform can then only recognize one of them.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic