Java: get the time of another time zone

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;
}

Erzeugen eines MD5 Hash

Hier eine simple methode, um zu einem String einen passenden MD5 hash zu generieren:

/**
* generates a md5 hash from a string
*
* @param input – the string the md5 has to generate from
* @return – a md5 hash as a a string
*/
public String getMD5Hash(String input) {
StringBuffer stringBuffer = new StringBuffer(BUFFER_CAPACITY);
try {
MessageDigest md5 = MessageDigest.getInstance(„MD5“);
md5.update(input.getBytes());
Formatter f = new Formatter(stringBuffer);
for (byte b : md5.digest()) {
f.format(„%02x“, b);
}
} catch (NoSuchAlgorithmException e) {
log.error(e.getMessage());
}
return stringBuffer.toString();
}

Java: timezone shift / Zeit in andere Zeitzone umrechnen

To calculate a time to another time zone, just

  1. get a timezone object for the new timezone
  2. register the timezone object to a formatter
  3. 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));