我需要存储没有时区信息的年月日信息。
这是一些代码:
private static void test() {
Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("US/Pacific"));
System.out.println("Before: " + cal.get(DAY_OF_MONTH));
//
long datestamp = toDatestamp(cal.getTimeInMillis());
long timestamp = toTimestamp(datestamp);
cal.setTimeInMillis(timestamp);
System.out.println("After: " + cal.get(DAY_OF_MONTH));
}
private static long toTimestamp(long datestamp) {
return TimeUnit.DAYS.toMillis(datestamp);
// return datestamp * DS_MULT;
}
private static long toDatestamp(long timestamp) {
return TimeUnit.MILLISECONDS.toDays(timestamp);
// return timestamp / DS_MULT;
}
// hours * minutes * seconds * milliseconds
private static long DS_MULT = 24 * 60 * 60 * 1000;
有两种方法,其中一种被注释掉了。但结果是一样的:
Before: 26
After: 25
为什么转换后日期会改变?我错过了一些明显的东西吗?