Skip to content

Instantly share code, notes, and snippets.

@kolov
Last active August 29, 2015 14:21
Show Gist options
  • Select an option

  • Save kolov/7cd1219290b84bc419c6 to your computer and use it in GitHub Desktop.

Select an option

Save kolov/7cd1219290b84bc419c6 to your computer and use it in GitHub Desktop.
Print date in different locales
package _;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.ISODateTimeFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
/**
* Getting and printing time with different locales.
*
* A sample output of this program is:
* -- java 7 time: ---
Date.toString in Amsterdam: Wed May 27 15:18:07 CEST 2015
Date.toString in Tokyo: Wed May 27 22:18:07 JST 2015
Time difference:-8ms
Amsterdam prints time in UTC 2015-05-27T01:18:07.480Z
Amsterdam prints time in Tokyo 2015-05-27T10:18:07.480Z
Amsterdam prints time in Amsterdam 2015-05-27T03:18:07.480Z
Tokyo prints time in UTC 2015-05-27T01:18:07.488Z
Tokyo prints time in Tokyo 2015-05-27T10:18:07.488Z
Tokyo prints time in Amsterdam 2015-05-27T03:18:07.488Z
-- Joda time: ---
DateTime.toString in Amsterdam: 2015-05-27T15:18:07.535+02:00
DateTime.toString in Tokyo : 2015-05-27T22:18:07.580+09:00
Amsterdam time in UTC: 2015-05-27T13:18:07.535Z
Tokyo time in UTC: 2015-05-27T13:18:07.580Z
Process finished with exit code 0
*/
public class Time {
public static void main(String[] args) {
printTimesWithJava7();
printTimeWithJoda();
}
private static void printTimesWithJava7() {
System.out.println("-- java 7 time: ---");
TimeZone tzAmsterdam = TimeZone.getTimeZone("Europe/Amsterdam");
TimeZone tzTokyo = TimeZone.getTimeZone("Asia/Tokyo");
TimeZone tzUtc = TimeZone.getTimeZone("UTC");
// a computer in Amsterdam gets the time and prints it
TimeZone.setDefault(tzAmsterdam);
Date nowAmsterdam = new Date();
System.out.println("Date.toString in Amsterdam: " + nowAmsterdam);
// a computer in Tokyo gets the time and prints it
TimeZone.setDefault(tzTokyo);
Date nowTokyo = new Date();
System.out.println("Date.toString in Tokyo: " + nowTokyo);
// The time difference is just the time consumed by println
System.out.println("Time difference:" + (nowAmsterdam.getTime() - nowTokyo.getTime()) + "ms");
// Prepare time formatters: UTC, Amsterdam, Tokyo
SimpleDateFormat formatUTC = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS'Z'");
formatUTC.setTimeZone(tzUtc);
SimpleDateFormat formatAmsterdam = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS'Z'");
formatAmsterdam.setTimeZone(tzAmsterdam);
SimpleDateFormat formatTokyo = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS'Z'");
formatTokyo.setTimeZone(tzTokyo);
//Amsterdam prints times
TimeZone.setDefault(tzAmsterdam);
System.out.println("Amsterdam prints time in UTC " + formatUTC.format(nowAmsterdam));
System.out.println("Amsterdam prints time in Tokyo " + formatTokyo.format(nowAmsterdam));
System.out.println("Amsterdam prints time in Amsterdam " + formatAmsterdam.format(nowAmsterdam));
//Tokyo prints times
TimeZone.setDefault(tzTokyo);
System.out.println("Tokyo prints time in UTC " + formatUTC.format(nowTokyo));
System.out.println("Tokyo prints time in Tokyo " + formatTokyo.format(nowTokyo));
System.out.println("Tokyo prints time in Amsterdam " + formatAmsterdam.format(nowTokyo));
}
private static void printTimeWithJoda() {
System.out.println("-- Joda time: ---");
DateTimeZone tzAmsterdam = DateTimeZone.forID("Europe/Amsterdam");
DateTimeZone tzTokyo = DateTimeZone.forID("Asia/Tokyo");
// a computer in Amsterdam gets the time and prints it
DateTime nowAmsterdam = new DateTime(tzAmsterdam);
System.out.println("DateTime.toString in Amsterdam: " + nowAmsterdam);
// a computer in Tokyo gets the time and prints it
DateTime nowTokyo = new DateTime(tzTokyo);
System.out.println("DateTime.toString in Tokyo : " + nowTokyo);
System.out.println("Amsterdam time in UTC: " +
ISODateTimeFormat.dateTime().print(nowAmsterdam.toInstant()));
System.out.println("Tokyo time in UTC: " +
ISODateTimeFormat.dateTime().print(nowTokyo.toInstant()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment