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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple calendar with TimeZone wouldn't work..

Status
Not open for further replies.

patnim17

Programmer
Jun 19, 2005
111
0
0
US
Hi,
I have this simple program in which want a Calendar Object created for a TimeZone...doesn't work..

import java.text.*;
import java.util.*;

public class TestTimeZone {
public static void main(String args[]) {
TimeZone tz1 = TimeZone.getTimeZone("America/Los_Angeles");
System.out.println("TimeZone id=" + tz1.getID());

Calendar gmtCal = Calendar.getInstance(tz1);
System.out.println("getTime=" + gmtCal.getInstance().getTime());

}
}
 
A quick google finds this workaround :
Code:
                 Calendar c = Calendar.getInstance();                 DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL);                 df.setTimeZone (TimeZone.getTimeZone("America/Los_Angeles"));                 String timeStamp = df.format (c.getTime ());                 System.out.println(timeStamp);
below_url said:
The problem is that the Date object that you extract from the Calendar (c.getTime()) loses the time zone information that you have in the Calendar. You can work around this problem by using the DateFormat class as follows:
Taken from
--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top