| 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
|
|
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.
|
 |
 |
|
|
subject: calendar.getTime()
|
|
|