• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Calendar class should not do this. I think.

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vijay,

Try this instead:

 
vijay shanker
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 28401
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic