The following code cannot pass the assertion under the Android emulator:
TimeZone timezone = TimeZone.getTimeZone("Hongkong");
Locale locale = new Locale("zh", "HK");
Calendar calendar = Calendar.getInstance(timezone, locale);
calendar.set(Calendar.YEAR, 1979);
calendar.set(Calendar.MONTH, 4);
calendar.set(Calendar.DAY_OF_MONTH, 13);
calendar.set(Calendar.HOUR_OF_DAY, 4);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.SECOND, 0);
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
df.setTimeZone(timezone);
String sDate = df.format(calendar.getTime());
int debug = calendar.get(Calendar.HOUR_OF_DAY);
Assert.assertEquals("Error datetime: " + sDate, 4, debug);
And I found that, if change the DAY_OF_MONTH to 12, the error is gone:
calendar.set(Calendar.DAY_OF_MONTH, 12);
Note that this problem is not exists on JUnit test. (without Android)
Finally, this problem seems may not be exists on Android real machine.
Any comments?
I believe, it’s because of this transition:
Here is the DST change which happened on 13th May 1979.
Since DST for Hong Kong went ON and OFF and finally OFF, it may be the root cause of the issue.