'When does java.util.Date set value to cdate field?
I debug in my code like this:
public static void main(String[] args) {
Date date = new Date();
System.out.println(date);
}
While seeing the source code, I find that it just set the value to fastTime.
public Date() {
this(System.currentTimeMillis());
}
public Date(long date) {
fastTime = date;
}
There is another field cdate field in java.util.Date, but how to set value to it?
/*
* If cdate is null, then fastTime indicates the time in millis.
* If cdate.isNormalized() is true, then fastTime and cdate are in
* synch. Otherwise, fastTime is ignored, and cdate indicates the
* time.
*/
private transient BaseCalendar.Date cdate;
[![debug pic]] [1]: https://i.stack.imgur.com/W2N7P.png
Solution 1:[1]
The implementation of PrintStream.println(Object) calls toString() for any object you pass into it.
Now if you look at the implementation of Date.toString() you see
public String toString() {
// "EEE MMM dd HH:mm:ss zzz yyyy";
BaseCalendar.Date date = normalize();
// format the contents of date
}
You can then further look at Date.normalize() and find
private final BaseCalendar.Date normalize() {
if (cdate == null) {
BaseCalendar cal = getCalendarSystem(fastTime);
cdate = (BaseCalendar.Date) cal.getCalendarDate(fastTime,
TimeZone.getDefaultRef());
return cdate;
}
// code to update cdate if it is already present
}
Please note that a debugger that displays the current value of a variable Date date also uses the Date.toString() method. It might therefore be difficult to trace the code path in Date.normalize() with cdate == null
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Thomas Kläger |
