• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calendar class should not do this. I think.  RSS feed

 
vijay shanker
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vijay,

Try this instead:

 
vijay shanker
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
vijay shanker
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 22827
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!