The Problem
GAE's system timezone is UTC and I need print date using the specified timezone and format.
The Solution
We can call DateFormat.setTimeZone to set it's timezone:
The test code is like below:
GAE's system timezone is UTC and I need print date using the specified timezone and format.
The Solution
We can call DateFormat.setTimeZone to set it's timezone:
public static String formatDate(Date date, String format, TimeZone tz) { DateFormat df = new SimpleDateFormat(format); df.setTimeZone(tz); return df.format(date); }To get get all available time zone ids: System.out.println(Arrays.toString(TimeZone.getAvailableIDs()));
The test code is like below:
@Test public void testDfWithTZ() { // To get all available time zone ids: System.out.println(Arrays.toString(TimeZone.getAvailableIDs())); Date date = new Date(); String format = "yyyy-MM-dd hh:mm:ss"; TimeZone tz = TimeZone.getTimeZone("US/Eastern"); String dateStr = formatDate(date, format, tz); System.out.println(dateStr); tz = TimeZone.getTimeZone("Asia/Shanghai"); dateStr = formatDate(date, format, tz); System.out.println(dateStr); // Get system's default timezone tz = TimeZone.getDefault(); dateStr = formatDate(date, format, tz); System.out.println(dateStr); }