public Date getLocalCurrentDate() {
TimeZone timeZone = TimeZone.getTimeZone(„Greece“);
TimeZone defaultZone = TimeZone.getTimeZone(„GMT“);
Date localDate = new Date();
Date yourDate = new Date();
yourDate.setTime(localDate.getTime() + timeZone.getRawOffset() – defaultZone.getRawOffset() – defaultZone.getDSTSavings());
return yourDate;
}
Schlagwort: time
Java: timezone shift / Zeit in andere Zeitzone umrechnen
To calculate a time to another time zone, just
- get a timezone object for the new timezone
- register the timezone object to a formatter
- recalculate the time with the formatter to a string
code sample:
// formatter
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);
// local date
Date now = new Date();
// instanciate timezone
TimeZone timezone = TimeZone.getTimeZone(„Japan“);
// register on formatter
df.setTimeZone(timezone);
// recalculate
System.out.println(„Japan time is:“+df.format(now));