Last active
November 27, 2017 19:15
-
-
Save smilingleo/8e8b5649ef2216fdad29772713198042 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The output is:
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.