my dog learned polymorphism
The moose likes Java in General and the fly likes calendar.getTime() Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "calendar.getTime()" Watch "calendar.getTime()" New topic
Author

calendar.getTime()

Sully Sylvester
Greenhorn

Joined: Jan 05, 2005
Posts: 6
I am trying to convert a time from one timezone to another


java.text.SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
formatter.setTimeZone();

String formattedDateTime = formatter.format(date);

parseDate(formattedDateTime);

// this all works ok and i get string with correct time in whatever timezone i put in setTimeZone();

private Date parseDate(String dateFormat)
{

Calendar cal = new GregorianCalendar();
cal.set(Calendar.YEAR, substring of dateFormat put to int);
cal.set(Calendar.MONTH, substring of dateFormat put to int);
cal.set(Calendar.DATE, substring of dateFormat put to int);
cal.set(Calendar.HOUR_OF_DAY, substring of dateFormat put to int);
cal.set(Calendar.MINUTE, substring of dateFormat put to int);

return cal.getTime();
}

Ok problem is that cal.getTime returns correct time but wrong timezone so date object returned will have correct time bubt timezone will be GMT so if i change result again to another timeZone it will think its GMT as opposed to its correct timeZone.

Any help appreciated.

Thanks,
Carl
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Converting Time Between Time Zones
Sully Sylvester
Greenhorn

Joined: Jan 05, 2005
Posts: 6
Thanks for that. unfortunately the result is mapped into a xml file and xsd requires it to be a date object so i have to call getTime or convert calendar at some point to a Date() and this is where i get my problems
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16482
    
    2

Well, Date objects don't have a timezone so the whole concept of "converting from one timezone to another" is misguided if that's what you need. Perhaps your question is really "I have a string that represents a time in timezone X and I want to create a Date object from that"? But it can't be because your code seems to start with a Date object. If that's the case then you don't need to do anything.

You can display that Date object as a string that interprets it in timezone X using a SimpleDateFormat, as you already know.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: calendar.getTime()
 
Similar Threads
Date and time componet
Woa! Why does Date/Calendar do this?
parse date string
Timezones
GMT time displaying