publicclass DaylightTimeTest { privatestaticfinalint ONE_HOUR = 60 * 60 * 1000; // one hour privatestaticfinalint INTERVAL = 24 * ONE_HOUR; // one day privatestaticfinal String[] ZONES = TimeZone.getAvailableIDs(); privatestaticint errors = 0;
publicstaticvoid main(String[] args) {
// Test default TimeZone for (String id : ZONES) {
TimeZone tz = TimeZone.getTimeZone(id); long now = System.currentTimeMillis(); boolean observes = tz.observesDaylightTime(); boolean found = findDSTTransition(tz, now); if (observes != found) { // There's a critical section. If DST ends after the // System.currentTimeMills() call, there should be // inconsistency in the determination. Try the same // thing again to see the inconsistency was due to the // critical section.
now = System.currentTimeMillis();
observes = tz.observesDaylightTime();
found = findDSTTransition(tz, now); if (observes != found) {
System.err.printf("%s: observesDaylightTime() should return %s at %d%n",
tz.getID(), found, now);
errors++;
}
}
}
// Test SimpleTimeZone in which observesDaylightTime() is // equivalent to useDaylightTime().
testSimpleTimeZone(new SimpleTimeZone(-8*ONE_HOUR, "X",
APRIL, 1, -SUNDAY, 2*ONE_HOUR,
OCTOBER, -1, SUNDAY, 2*ONE_HOUR, 1*ONE_HOUR));
testSimpleTimeZone(new SimpleTimeZone(-8*ONE_HOUR, "Y"));
if (errors > 0) { thrownew RuntimeException("DaylightTimeTest: failed");
}
}
/** *Returnstrueifit's`now'inDSTorthere'sany *standard-to-daylighttransitionwithin50yearsafter`now'.
*/ privatestaticboolean findDSTTransition(TimeZone tz, long now) {
GregorianCalendar cal = new GregorianCalendar(tz, Locale.US);
cal.setTimeInMillis(now);
cal.add(YEAR, 50); long end = cal.getTimeInMillis();
for (long t = now; t < end; t += INTERVAL) {
cal.setTimeInMillis(t); if (cal.get(DST_OFFSET) > 0) { returntrue;
}
} returnfalse;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.