Java Time Zone: It’s 9 o’clock here, what’s your time ?

	private static void printTimeMain() throws ParseException {
		// the computer which runs this program is of the time zone of China
		long someSummerMilis = DateUtils.parseDate("2015-7-4 16:00:00",
				new String[] { "yyyy-MM-dd HH:mm:ss" }).getTime();
		long someWinderMilis = DateUtils.parseDate("2015-11-11 16:00:00",
				new String[] { "yyyy-MM-dd HH:mm:ss" }).getTime();

		System.out
				.println("==============when it is 2015-7-4 16:00:00  in China, the world's times are==============");

		printTime(someSummerMilis, "GMT+8");
		printTime(someSummerMilis, "America/Los_Angeles");
		printTime(someSummerMilis, "GMT-8");
		printTime(someSummerMilis, "Asia/Shanghai");

		System.out
				.println("==============When it is 2015-11-11 16:00:00  in China, the world's times are==============");

		printTime(someSummerMilis, "GMT+8");
		printTime(someSummerMilis, "GMT-8");
		printTime(someWinderMilis, "America/Los_Angeles");
		printTime(someWinderMilis, "Asia/Shanghai");
	}

	private static void printTime(long milis, String timeZoneId) {
		GregorianCalendar calendar = new GregorianCalendar(
				TimeZone.getTimeZone(timeZoneId));
		calendar.setTimeInMillis(milis);

		String s = calendar.getTimeZone().getDisplayName() + "\t"
				+ calendar.get(Calendar.YEAR) + "-"
				+ (calendar.get(Calendar.MONTH) + 1) + "-"
				+ calendar.get(Calendar.DAY_OF_MONTH) + " "
				+ calendar.get(Calendar.HOUR_OF_DAY) + ":"
				+ calendar.get(Calendar.MINUTE) + ":"
				+ calendar.get(Calendar.SECOND) + "\t Day Light Saving Hours ="
				+ (double) calendar.getTimeZone().getDSTSavings() / 3600000;
		System.out.println(s);
	}

 output: 

==============when it is 2015-7-4 16:00:00  in China, the world's times are==============
GMT+08:00	            2015-7-4 16:0:0	 Day Light Saving Hours =0.0
Pacific Standard Time	2015-7-4 1:0:0	 Day Light Saving Hours =1.0
GMT-08:00	            2015-7-4 0:0:0	 Day Light Saving Hours =0.0
China Standard Time	2015-7-4 16:0:0	 Day Light Saving Hours =0.0

==============When it is 2015-11-11 16:00:00  in China, the world's times are==============
GMT+08:00	            2015-7-4 16:0:0	     Day Light Saving Hours =0.0
GMT-08:00	            2015-7-4 0:0:0	     Day Light Saving Hours =0.0
Pacific Standard Time	2015-11-11 0:0:0	 Day Light Saving Hours =1.0
China Standard Time	    2015-11-11 16:0:0	 Day Light Saving Hours =0.0


Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.