• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

calendar.getTime()  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Converting Time Between Time Zones
 
Sully Sylvester
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 24464
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Power corrupts. Absolute power xxxxxxxxxxxxxxxx is kinda neat.
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!