Skip to content

Instantly share code, notes, and snippets.

@smilingleo
Last active November 27, 2017 19:15
Show Gist options
  • Save smilingleo/8e8b5649ef2216fdad29772713198042 to your computer and use it in GitHub Desktop.
Save smilingleo/8e8b5649ef2216fdad29772713198042 to your computer and use it in GitHub Desktop.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
/**
* This is a test indicating the timezone issue for a datebase which is in a timezone which observes
* DST,
* for example, America/Los_Angeles.
*
* @author leo
*
*/
public class TimeZoneTest {
static TimeZone ptTz = TimeZone.getTimeZone("America/Los_Angeles");
static TimeZone bjTz = TimeZone.getTimeZone("GMT+08");
static TimeZone utc = TimeZone.getTimeZone("UTC");
static SimpleDateFormat ptFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
static SimpleDateFormat bjFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
static {
ptFormatter.setTimeZone(ptTz);
bjFormatter.setTimeZone(bjTz);
}
public static void main(String[] args) throws ParseException {
System.out.println("from utc to bj time");
// pst -> pdt
System.out.println("pst -> pdt");
testToBJ(utc, 2016, 2, 13, 9, 00, 00);
testToBJ(utc, 2016, 2, 13, 10, 00, 00);
testToBJ(utc, 2016, 2, 13, 11, 00, 00);
// pdt -> pst
System.out.println("pdt -> pst");
testToBJ(utc, 2016, 10, 6, 9, 00, 00);
testToBJ(utc, 2016, 10, 6, 10, 00, 00);
testToBJ(utc, 2016, 10, 6, 11, 00, 00);
System.out.println("\nfrom pt to bj time");
// pst -> pdt
System.out.println("pst -> pdt");
testToBJ(ptTz, 2016, 2, 13, 1, 00, 00);
// the problem.
testToBJ(ptTz, 2016, 2, 13, 2, 00, 00);
testToBJ(ptTz, 2016, 2, 13, 3, 00, 00);
// pst -> pdt
System.out.println("pdt -> pst");
testToBJ(ptTz, 2016, 10, 6, 1, 00, 00);
testToBJ(ptTz, 2016, 10, 6, 2, 00, 00);
testToBJ(ptTz, 2016, 10, 6, 3, 00, 00);
}
/**
* Simulate the process by which we parse a datetime input from UI, persist it into DB, and
* render it as a datetime in GMT+08 on UI
*
* @param tz
* @param year
* @param month
* @param day
* @param hour
* @param minute
* @param sec
* @throws ParseException
*/
protected static void testToBJ(TimeZone tz, int year, int month, int day, int hour, int minute, int sec) throws ParseException {
Calendar cal = Calendar.getInstance(tz);
// -> PDT
cal.set(year, month, day, hour, minute, sec);
String inDB = ptFormatter.format(cal.getTime());
Date dt1 = ptFormatter.parse(inDB);
// render on UI
String onUI = bjFormatter.format(dt1);
String str = String.format("%s: %d-%d-%dT%d:%d:%d -> DB: %s -> UI: %s", tz.getID(), year, month + 1, day, hour, minute, sec, inDB, onUI);
System.out.println(str);
}
}
@smilingleo
Copy link
Author

The output is:

from utc to bj time
pst -> pdt
UTC: 2016-3-13T9:0:0  -> DB: 2016-03-13T01:00:00 -> UI: 2016-03-13T17:00:00
UTC: 2016-3-13T10:0:0  -> DB: 2016-03-13T03:00:00 -> UI: 2016-03-13T18:00:00
UTC: 2016-3-13T11:0:0  -> DB: 2016-03-13T04:00:00 -> UI: 2016-03-13T19:00:00
pdt -> pst
UTC: 2016-11-6T9:0:0  -> DB: 2016-11-06T01:00:00 -> UI: 2016-11-06T17:00:00
UTC: 2016-11-6T10:0:0  -> DB: 2016-11-06T02:00:00 -> UI: 2016-11-06T18:00:00
UTC: 2016-11-6T11:0:0  -> DB: 2016-11-06T03:00:00 -> UI: 2016-11-06T19:00:00

from pt to bj time
pst -> pdt
America/Los_Angeles: 2016-3-13T1:0:0  -> DB: 2016-03-13T01:00:00 -> UI: 2016-03-13T17:00:00
America/Los_Angeles: 2016-3-13T2:0:0  -> DB: 2016-03-13T03:00:00 -> UI: 2016-03-13T18:00:00
America/Los_Angeles: 2016-3-13T3:0:0  -> DB: 2016-03-13T03:00:00 -> UI: 2016-03-13T18:00:00
pdt -> pst
America/Los_Angeles: 2016-11-6T1:0:0  -> DB: 2016-11-06T01:00:00 -> UI: 2016-11-06T17:00:00
America/Los_Angeles: 2016-11-6T2:0:0  -> DB: 2016-11-06T02:00:00 -> UI: 2016-11-06T18:00:00
America/Los_Angeles: 2016-11-6T3:0:0  -> DB: 2016-11-06T03:00:00 -> UI: 2016-11-06T19:00:00

You can see, for the conversion from PT to BJ time, two different datetimes like 2016-03-13T02:00:00-08, 2016-03-13T02:00:00-07 are rendered as same datetime on UI.
Which is because a datetime in DB, for example, Mysql datetime datatype which missed the timezone info, will be parsed with system timezone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment