File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Calendar Conversions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calendar Conversions" Watch "Calendar Conversions" New topic

Calendar Conversions


Joined: Jul 12, 2001
Posts: 1
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);
//This will get the time in Paris, France
Calendar parisTime =
Date parisDate = parisTime.getTime();
String parisDateString = formatter.format(parisDate);
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??
Frank Carver

Joined: Jan 07, 1999
Posts: 6920
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at . 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.

Read about me at ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Kaspar Dahlqvist
Ranch Hand

Joined: Jun 18, 2001
Posts: 128
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.
Help, please!!!
Eric Edwards
Ranch Hand

Joined: Feb 12, 2000
Posts: 60
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!
[This message has been edited by Eric Edwards (edited July 13, 2001).]
Kaspar Dahlqvist
Ranch Hand

Joined: Jun 18, 2001
Posts: 128
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...
Buck Webb

Joined: Jul 13, 2001
Posts: 1
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!!!
Roy Tock
Ranch Hand

Joined: Jul 16, 2001
Posts: 83
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:

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.
I agree. Here's the link:
subject: Calendar Conversions
It's not a secret anymore!