• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

calendar.getTime()

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Converting Time Between Time Zones
 
Sully Sylvester
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 28329
97
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, 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.
 
Who knew that furniture could be so violent? Put this tiny ad out there to see what happens:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic