Java Calendar is corrupted by setting HOUR_OF_DAY -
question: consider bug in java.util.calendar ?
a calendar object in local (pdt) time zone instantiated , assigned 0 (epoch start) date. value maintained, expected after setting 0 calendar's millisecond, second, , minute. however, once hour of day set zero, time becomes non-zero. acquires value -57600000ms = -16 hours. may timezone bug, value -16 hours not correspond local -7 hours (pdt) offset, in effect @ time of execution. without daylight savings, time offset pst (-8 hours) , not correspond -16 hours.
if setting hour should affect time zone offsets, expect setting minute should because: ( https://en.wikipedia.org/wiki/time_zone ) several places "use half-hour deviations standard time, , some" ... "use quarter-hour deviations."
the code:
date epochstart = new date(0l); system.out.println("epochstart=" + epochstart.gettime()); calendar calendar = calendar.getinstance(); calendar.settime(epochstart); system.out.println("epochstart in calendar=" + calendar.gettime().gettime()); calendar.set(calendar.millisecond, 0); system.out.println("ms cleared in calendar=" + calendar.gettime().gettime()); calendar.set(calendar.second, 0); system.out.println("second cleared in calendar=" + calendar.gettime().gettime()); calendar.set(calendar.minute, 0); system.out.println("minute cleared in calendar=" + calendar.gettime().gettime()); calendar.set(calendar.hour_of_day, 0); system.out.println("hourofday cleared in calendar=" + calendar.gettime().gettime());
the output:
epochstart=0 epochstart in calendar=0 ms cleared in calendar=0 second cleared in calendar=0 minute cleared in calendar=0 hourofday cleared in calendar=-57600000
i have found descriptions of other bugs in java.util.calendar, don't think known.
-- time.
java fine , working expected.
a java.util.date
moment in time, internally represented milliseconds since start of epoch (january 1, 1970 @ 00:00:00 utc).
java.util.calender
manipulating time information in timezone dependent format. normally, timezone implicitly defined through current locale. calling calendar.settime(xxx)
takes supplied moment in time , converts calendar fields (year, month, day, hour, minute, ...) according timezone.
in case, start of epoch split dec 31, 1969, 16:00:00.000 pdt
. clearing milliseconds, seconds , minutes fields has no effect, since these fields zero.
however clearing hours field changes timestamp in case, start of epoch split dec 31, 1969, 00:00:00.000 pdt
.
the following code shows manipulations, converting calendars timestamp utc , pdt date strings.
import java.text.simpledateformat; import java.util.*; public class calendartest { public static void main(string[] args) { timezone timezone = timezone.gettimezone("america/los_angeles"); simpledateformat udf = new simpledateformat("'utc: 'yyyy-mm-dd hh:mm:ss.sss z"); udf.settimezone(timezone.gettimezone("utc")); simpledateformat df = new simpledateformat("'pdt: 'yyyy-mm-dd hh:mm:ss.sss z"); df.settimezone(timezone); date epochstart = new date(0l); system.out.println("epochstart=" + epochstart.gettime()); system.out.println(udf.format(epochstart)); system.out.println(df.format(epochstart)); system.out.println(); calendar calendar = calendar.getinstance(timezone); calendar.settime(epochstart); system.out.println("epochstart in calendar=" + calendar.gettime().gettime()); system.out.println(udf.format(calendar.gettime())); system.out.println(df.format(calendar.gettime())); system.out.println(); calendar.set(calendar.millisecond, 0); system.out.println("ms cleared in calendar=" + calendar.gettime().gettime()); system.out.println(udf.format(calendar.gettime())); system.out.println(df.format(calendar.gettime())); system.out.println(); calendar.set(calendar.second, 0); system.out.println("second cleared in calendar=" + calendar.gettime().gettime()); system.out.println(udf.format(calendar.gettime())); system.out.println(df.format(calendar.gettime())); system.out.println(); calendar.set(calendar.minute, 0); system.out.println("minute cleared in calendar=" + calendar.gettime().gettime()); system.out.println(udf.format(calendar.gettime())); system.out.println(df.format(calendar.gettime())); system.out.println(); calendar.set(calendar.hour_of_day, 0); system.out.println("hourofday cleared in calendar=" + calendar.gettime().gettime()); system.out.println(udf.format(calendar.gettime())); system.out.println(df.format(calendar.gettime())); system.out.println(); } }
Comments
Post a Comment