File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Calendar class should not do this. I think. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Calendar class should not do this. I think." Watch "Calendar class should not do this. I think." New topic
Author

Calendar class should not do this. I think.

vijay shanker
Ranch Hand

Joined: Oct 26, 2007
Posts: 88
Hi all,
i create a calendar object for the given time zone say "HongKong" but when i try to read the time from the calendar object it returns the time in the mylocal time zone.This thing is strange to me why calendar object to to behave like this.if i want to get the time in the time zone of the calendar object what should i do i am not getting the idea any where or doing thing i want.
<code>

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.SimpleTimeZone;
import java.util.TimeZone;

/**
* @author Vijay Shanker
*
*/
public class CalendarConditions {

public static void main(String[] args) {

CalendarConditions conditions = new CalendarConditions();

conditions.createDateFromCalendar();

}

public void createDateFromCalendar() {
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("Hongkong"));

System.out.println(cal.getTime());
}

}

</code>
Ritvick Nigam
Greenhorn

Joined: Jun 06, 2008
Posts: 7
Hi Vijay,

Try this instead:

vijay shanker
Ranch Hand

Joined: Oct 26, 2007
Posts: 88
thanks fro reply

i have worked on this but actually i need is a java.util.Date object


cheers

vijay shanker
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12952
    
    3

Calendar.getTime() returns a Date object.

The problem is this: Class Date doesn't know anything about timezones. A Date object only contains the number of milliseconds since a particular point in time (January 1, 1970, 12:00 AM GMT). If you print a Date object with System.out.println(), then it will be printed in the default timezone of the computer - which is your local time zone.

If you want to print the date and time in a particular timezone, then use a date formatter class such as SimpleDateFormat, and set the timezone on the formatter object to let it know in which timezone you want the date to be displayed. See Ritvick Nigam's example.

If you get the Date object from the Calendar object, then you loose the timezone information.
[ June 12, 2008: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
vijay shanker
Ranch Hand

Joined: Oct 26, 2007
Posts: 88
hi Jesper,
actually i don't have to print the value of the date object.

what i have to do is pass this information to another component that only can entertain the java.util.Date object. that is why i got to change the Calendar object to Date object.

so if this is the situation will you please suggest me some idea to implement and yes i realy need the timezone info other side of the system.

cheers
vijay shanker
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16487
    
    2

Well, as you've been told a couple of times now, a Date object doesn't have a timezone. So if you need timezone information in your system, wouldn't the obvious solution be to use a TimeZone object as well? Or a String which can be turned into a TimeZone object?
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Calendar class should not do this. I think.
 
Similar Threads
Help:Error with Servlets !!
TimeStamp.toString() does not work as expected
Wrong time zones...
Problem with Calendar().getTime()
Change Timezone from GMT to EST, the best way to accomplish?