下面列出了java.util.GregorianCalendar#clear ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void main(String args[]) {
GregorianCalendar cal = new GregorianCalendar();
cal.clear();
cal.set(YEAR, 2003);
long t = cal.getTime().getTime();
// For the time calculation, the MONTH and DAY_OF_MONTH fields
// (with the default values) have been used for determining
// the date. However, both the MONTH and DAY_OF_MONTH fields
// should be kept UNSET after the time calculation.
if (cal.isSet(MONTH) || cal.isSet(DAY_OF_MONTH)) {
throw new RuntimeException("After getTime(): MONTH field=" + cal.isSet(MONTH)
+ ", DAY_OF_MONTH field=" + cal.isSet(DAY_OF_MONTH));
}
// After calling get() for any field, all field values are
// recalculated and their field states are set to
// COMPUTED. isSet() must return true.
int y = cal.get(YEAR);
if (!(cal.isSet(MONTH) && cal.isSet(DAY_OF_MONTH))) {
throw new RuntimeException("After get(): MONTH field=" + cal.isSet(MONTH)
+ ", DAY_OF_MONTH field=" + cal.isSet(DAY_OF_MONTH));
}
}
public static void main(String args[]) {
GregorianCalendar cal = new GregorianCalendar();
cal.clear();
cal.set(YEAR, 2003);
long t = cal.getTime().getTime();
// For the time calculation, the MONTH and DAY_OF_MONTH fields
// (with the default values) have been used for determining
// the date. However, both the MONTH and DAY_OF_MONTH fields
// should be kept UNSET after the time calculation.
if (cal.isSet(MONTH) || cal.isSet(DAY_OF_MONTH)) {
throw new RuntimeException("After getTime(): MONTH field=" + cal.isSet(MONTH)
+ ", DAY_OF_MONTH field=" + cal.isSet(DAY_OF_MONTH));
}
// After calling get() for any field, all field values are
// recalculated and their field states are set to
// COMPUTED. isSet() must return true.
int y = cal.get(YEAR);
if (!(cal.isSet(MONTH) && cal.isSet(DAY_OF_MONTH))) {
throw new RuntimeException("After get(): MONTH field=" + cal.isSet(MONTH)
+ ", DAY_OF_MONTH field=" + cal.isSet(DAY_OF_MONTH));
}
}
public static void main(String args[]) {
GregorianCalendar cal = new GregorianCalendar();
cal.clear();
cal.set(YEAR, 2003);
long t = cal.getTime().getTime();
// For the time calculation, the MONTH and DAY_OF_MONTH fields
// (with the default values) have been used for determining
// the date. However, both the MONTH and DAY_OF_MONTH fields
// should be kept UNSET after the time calculation.
if (cal.isSet(MONTH) || cal.isSet(DAY_OF_MONTH)) {
throw new RuntimeException("After getTime(): MONTH field=" + cal.isSet(MONTH)
+ ", DAY_OF_MONTH field=" + cal.isSet(DAY_OF_MONTH));
}
// After calling get() for any field, all field values are
// recalculated and their field states are set to
// COMPUTED. isSet() must return true.
int y = cal.get(YEAR);
if (!(cal.isSet(MONTH) && cal.isSet(DAY_OF_MONTH))) {
throw new RuntimeException("After get(): MONTH field=" + cal.isSet(MONTH)
+ ", DAY_OF_MONTH field=" + cal.isSet(DAY_OF_MONTH));
}
}
public void Test4031502() {
// This bug actually occurs on Windows NT as well, and doesn't
// require the host zone to be set; it can be set in Java.
String[] ids = TimeZone.getAvailableIDs();
boolean bad = false;
for (int i = 0; i < ids.length; ++i) {
TimeZone zone = TimeZone.getTimeZone(ids[i]);
GregorianCalendar cal = new GregorianCalendar(zone);
cal.clear();
cal.set(1900, 15, 5, 5, 8, 13);
if (cal.get(HOUR) != 5) {
logln(zone.getID() + " "
+ //zone.useDaylightTime() + " "
+ cal.get(DST_OFFSET) / (60 * 60 * 1000) + " "
+ zone.getRawOffset() / (60 * 60 * 1000)
+ ": HOUR = " + cal.get(HOUR));
bad = true;
}
}
if (bad) {
errln("TimeZone problems with GC");
}
}
public void testToDate_summer_Zone() {
LocalDateTime base = new LocalDateTime(2005, 7, 9, 10, 20, 30, 40, COPTIC_PARIS);
Date test = base.toDate(TimeZone.getDefault());
check(base, 2005, 7, 9, 10, 20, 30, 40);
GregorianCalendar gcal = new GregorianCalendar();
gcal.clear();
gcal.set(Calendar.YEAR, 2005);
gcal.set(Calendar.MONTH, Calendar.JULY);
gcal.set(Calendar.DAY_OF_MONTH, 9);
gcal.set(Calendar.HOUR_OF_DAY, 10);
gcal.set(Calendar.MINUTE, 20);
gcal.set(Calendar.SECOND, 30);
gcal.set(Calendar.MILLISECOND, 40);
assertEquals(gcal.getTime(), test);
}
public void testToDate_winter_Zone() {
LocalDateTime base = new LocalDateTime(2005, 1, 9, 10, 20, 30, 40, COPTIC_PARIS);
Date test = base.toDate(TimeZone.getDefault());
check(base, 2005, 1, 9, 10, 20, 30, 40);
GregorianCalendar gcal = new GregorianCalendar();
gcal.clear();
gcal.set(Calendar.YEAR, 2005);
gcal.set(Calendar.MONTH, Calendar.JANUARY);
gcal.set(Calendar.DAY_OF_MONTH, 9);
gcal.set(Calendar.HOUR_OF_DAY, 10);
gcal.set(Calendar.MINUTE, 20);
gcal.set(Calendar.SECOND, 30);
gcal.set(Calendar.MILLISECOND, 40);
assertEquals(gcal.getTime(), test);
}
/**
* java.util.GregorianCalendar#computeTime()
*/
public void test_computeTime() {
// Regression for Harmony-493
GregorianCalendar g = new GregorianCalendar(
TimeZone.getTimeZone("Europe/London"),
new Locale("en", "GB")
);
g.clear();
g.set(2006, Calendar.MARCH, 26, 01, 50, 00);
assertEquals(1143337800000L, g.getTimeInMillis());
GregorianCalendar g1 = new GregorianCalendar(
TimeZone.getTimeZone("Europe/Moscow"));
g1.clear();
g1.set(2006, Calendar.MARCH, 26, 02, 20, 00);
assertEquals(1143328800000L, g1.getTimeInMillis());
assertEquals(3, g1.get(Calendar.HOUR_OF_DAY));
assertEquals(20, g1.get(Calendar.MINUTE));
g1.clear();
g1.set(2006, Calendar.OCTOBER, 29, 02, 50, 00);
assertEquals(1162079400000L, g1.getTimeInMillis());
assertEquals(2, g1.get(Calendar.HOUR_OF_DAY));
assertEquals(50, g1.get(Calendar.MINUTE));
// End of regression test
}
/**
* Builds a {@link DateTimeValue} object from the given data.
* @param millisFromEpoch the number of milliseconds from the epoch
* @param zone the timezone the number of milliseconds is in
* @return the {@link DateTimeValue} object
*/
public static DateTimeValue toDateTimeValue(long millisFromEpoch, TimeZone zone) {
GregorianCalendar c = new GregorianCalendar(zone);
c.clear();
c.setTimeInMillis(millisFromEpoch);
//@formatter:off
return new DateTimeValueImpl (
c.get(Calendar.YEAR),
c.get(Calendar.MONTH) + 1,
c.get(Calendar.DAY_OF_MONTH),
c.get(Calendar.HOUR_OF_DAY),
c.get(Calendar.MINUTE),
c.get(Calendar.SECOND)
);
//@formatter:on
}
@Ignore // TODO ion-java#165
@Test
public void testCustomCalendarLeapYearWithChainedAdd() {
// Create a purely Julian calendar, where leap years were every four years.
GregorianCalendar julianCalendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
julianCalendar.setGregorianChange(new Date(Long.MAX_VALUE));
julianCalendar.clear();
// 1800 is not a leap year under the Gregorian calendar, but it is under the Julian calendar.
julianCalendar.set(1799, Calendar.JANUARY, 27, 22, 58, 58);
julianCalendar.set(Calendar.MILLISECOND, 999);
Timestamp timestamp1 = Timestamp.forCalendar(julianCalendar);
Timestamp timestamp2 = timestamp1.addYear(1).addMonth(1).addDay(1).addHour(1).addMinute(1).addSecond(1).addMillis(1);
assertEquals("1800-02-29T00:00:00.000Z", timestamp2.toString());
}
@Ignore // TODO ion-java#165
@Test
public void testCustomCalendarLeapYearWithChainedAdjust() {
// Create a purely Julian calendar, where leap years were every four years.
GregorianCalendar julianCalendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
julianCalendar.setGregorianChange(new Date(Long.MAX_VALUE));
julianCalendar.clear();
// 1800 is not a leap year under the Gregorian calendar, but it is under the Julian calendar.
julianCalendar.set(1799, Calendar.JANUARY, 27, 22, 58, 58);
julianCalendar.set(Calendar.MILLISECOND, 999);
Timestamp timestamp1 = Timestamp.forCalendar(julianCalendar);
Timestamp timestamp2 = timestamp1.adjustYear(1).adjustMonth(1).adjustDay(1).adjustHour(1).adjustMinute(1).adjustSecond(1).adjustMillis(1);
assertEquals("1800-02-29T00:00:00.000Z", timestamp2.toString());
}
/**
* A test for the setRange() method (because the axis shows whole time
* periods, the range set for the axis will most likely be wider than the
* one specified).
*/
@Test
public void test2490803() {
Locale savedLocale = Locale.getDefault();
TimeZone savedTimeZone = TimeZone.getDefault();
try {
Locale.setDefault(Locale.FRANCE);
TimeZone.setDefault(TimeZone.getTimeZone("Europe/Paris"));
GregorianCalendar c0 = new GregorianCalendar();
c0.clear();
/* c0.set(2009, Calendar.JANUARY, 16, 12, 34, 56);
System.out.println(c0.getTime().getTime());
c0.clear();
c0.set(2009, Calendar.JANUARY, 17, 12, 34, 56);
System.out.println(c0.getTime().getTime()); */
PeriodAxis axis = new PeriodAxis("TestAxis");
axis.setRange(new Range(1232105696000L, 1232192096000L), false,
false);
Range r = axis.getRange();
Day d0 = new Day(16, 1, 2009);
Day d1 = new Day(17, 1, 2009);
assertEquals(d0.getFirstMillisecond(), r.getLowerBound(), EPSILON);
assertEquals(d1.getLastMillisecond() + 1.0, r.getUpperBound(),
EPSILON);
}
finally {
TimeZone.setDefault(savedTimeZone);
Locale.setDefault(savedLocale);
}
}
public void testToDate_winter() {
LocalDate base = new LocalDate(2005, 1, 9, COPTIC_PARIS);
Date test = base.toDate();
check(base, 2005, 1, 9);
GregorianCalendar gcal = new GregorianCalendar();
gcal.clear();
gcal.set(Calendar.YEAR, 2005);
gcal.set(Calendar.MONTH, Calendar.JANUARY);
gcal.set(Calendar.DAY_OF_MONTH, 9);
assertEquals(gcal.getTime(), test);
}
@Ignore // TODO ion-java#165
@Test
public void testCustomCalendarLeapYearWithChainedAdjustLessPrecise() {
// Create a purely Julian calendar, where leap years were every four years.
GregorianCalendar julianCalendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
julianCalendar.setGregorianChange(new Date(Long.MAX_VALUE));
julianCalendar.clear();
julianCalendar.set(1799, Calendar.JANUARY, 28);
Timestamp timestamp1 = Timestamp.forCalendar(julianCalendar);
Timestamp timestamp2 = timestamp1.adjustYear(1).adjustMonth(1).adjustDay(1).adjustHour(1).adjustMinute(1).adjustSecond(1).adjustMillis(1);
assertEquals("1800-02-29", timestamp2.toString());
}
@Ignore // TODO ion-java#165
@Test
public void testCustomCalendarLeapYear() {
// Create a purely Julian calendar, where leap years were every four years.
GregorianCalendar julianCalendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
julianCalendar.setGregorianChange(new Date(Long.MAX_VALUE));
julianCalendar.clear();
// 1800 is not a leap year under the Gregorian calendar, but it is under the Julian calendar.
julianCalendar.set(1800, Calendar.FEBRUARY, 28);
Timestamp timestamp1 = Timestamp.forCalendar(julianCalendar);
Timestamp timestamp2 = timestamp1.addDay(1);
assertEquals("1800-02-29", timestamp2.toString());
}
/**
* A test for the setRange() method (because the axis shows whole time
* periods, the range set for the axis will most likely be wider than the
* one specified).
*/
@Test
public void test2490803() {
Locale savedLocale = Locale.getDefault();
TimeZone savedTimeZone = TimeZone.getDefault();
try {
Locale.setDefault(Locale.FRANCE);
TimeZone.setDefault(TimeZone.getTimeZone("Europe/Paris"));
GregorianCalendar c0 = new GregorianCalendar();
c0.clear();
/* c0.set(2009, Calendar.JANUARY, 16, 12, 34, 56);
System.out.println(c0.getTime().getTime());
c0.clear();
c0.set(2009, Calendar.JANUARY, 17, 12, 34, 56);
System.out.println(c0.getTime().getTime()); */
PeriodAxis axis = new PeriodAxis("TestAxis");
axis.setRange(new Range(1232105696000L, 1232192096000L), false,
false);
Range r = axis.getRange();
Day d0 = new Day(16, 1, 2009);
Day d1 = new Day(17, 1, 2009);
assertEquals(d0.getFirstMillisecond(), r.getLowerBound(), EPSILON);
assertEquals(d1.getLastMillisecond() + 1.0, r.getUpperBound(),
EPSILON);
}
finally {
TimeZone.setDefault(savedTimeZone);
Locale.setDefault(savedLocale);
}
}
/**
* A test for the setRange() method (because the axis shows whole time
* periods, the range set for the axis will most likely be wider than the
* one specified).
*/
@Test
public void test2490803() {
Locale savedLocale = Locale.getDefault();
TimeZone savedTimeZone = TimeZone.getDefault();
try {
Locale.setDefault(Locale.FRANCE);
TimeZone.setDefault(TimeZone.getTimeZone("Europe/Paris"));
GregorianCalendar c0 = new GregorianCalendar();
c0.clear();
/* c0.set(2009, Calendar.JANUARY, 16, 12, 34, 56);
System.out.println(c0.getTime().getTime());
c0.clear();
c0.set(2009, Calendar.JANUARY, 17, 12, 34, 56);
System.out.println(c0.getTime().getTime()); */
PeriodAxis axis = new PeriodAxis("TestAxis");
axis.setRange(new Range(1232105696000L, 1232192096000L), false,
false);
Range r = axis.getRange();
Day d0 = new Day(16, 1, 2009);
Day d1 = new Day(17, 1, 2009);
assertEquals(d0.getFirstMillisecond(), r.getLowerBound(), EPSILON);
assertEquals(d1.getLastMillisecond() + 1.0, r.getUpperBound(),
EPSILON);
}
finally {
TimeZone.setDefault(savedTimeZone);
Locale.setDefault(savedLocale);
}
}
private void checkClear(GregorianCalendar cal, int expectedHourOfDay, long expectedMillis) {
cal.set(Calendar.YEAR, 1970);
cal.set(Calendar.MONTH, Calendar.JANUARY);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.clear(Calendar.HOUR_OF_DAY);
cal.clear(Calendar.HOUR);
cal.clear(Calendar.MINUTE);
cal.clear(Calendar.SECOND);
cal.clear(Calendar.MILLISECOND);
// Now we have a mix of set and unset fields.
assertTrue(cal.isSet(Calendar.DAY_OF_MONTH));
assertFalse(cal.isSet(Calendar.HOUR_OF_DAY));
// When we call get, unset fields are computed.
assertEquals(expectedHourOfDay, cal.get(Calendar.HOUR_OF_DAY));
// And set fields stay the same.
assertEquals(1, cal.get(Calendar.DAY_OF_MONTH));
// ...so now everything is set.
assertTrue(cal.isSet(Calendar.DAY_OF_MONTH));
assertTrue(cal.isSet(Calendar.HOUR_OF_DAY));
assertEquals(expectedMillis, cal.getTimeInMillis());
cal.set(Calendar.HOUR_OF_DAY, 1);
assertEquals(32400000, cal.getTimeInMillis());
}
private GregorianCalendar getCal() {
final GregorianCalendar cal = this.cal;
if (cal != null) {
cal.clear();
return cal;
}
return (this.cal = ClientSharedData.getDefaultCleanCalendar());
}
/**
* Test the mapping between millis and fields. For the purposes
* of this test, we don't care about timezones and week data
* (first day of week, minimal days in first week).
*/
@SuppressWarnings("deprecation")
public void TestMapping() {
TimeZone saveZone = TimeZone.getDefault();
int[] DATA = {
// Julian# Year Month DOM JULIAN:Year Month, DOM
2440588, 1970, JANUARY, 1, 1969, DECEMBER, 19,
2415080, 1900, MARCH, 1, 1900, FEBRUARY, 17,
2451604, 2000, FEBRUARY, 29, 2000, FEBRUARY, 16,
2452269, 2001, DECEMBER, 25, 2001, DECEMBER, 12,
2416526, 1904, FEBRUARY, 15, 1904, FEBRUARY, 2,
2416656, 1904, JUNE, 24, 1904, JUNE, 11,
1721426, 1, JANUARY, 1, 1, JANUARY, 3,
2000000, 763, SEPTEMBER, 18, 763, SEPTEMBER, 14,
4000000, 6239, JULY, 12, 6239, MAY, 28,
8000000, 17191, FEBRUARY, 26, 17190, OCTOBER, 22,
10000000, 22666, DECEMBER, 20, 22666, JULY, 5};
try {
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
Date PURE_GREGORIAN = new Date(Long.MIN_VALUE);
Date PURE_JULIAN = new Date(Long.MAX_VALUE);
GregorianCalendar cal = new GregorianCalendar();
for (int i = 0; i < DATA.length; i += 7) {
int julian = DATA[i];
int year = DATA[i + 1];
int month = DATA[i + 2];
int dom = DATA[i + 3];
int year2, month2, dom2;
long millis = ((long) julian - EPOCH_JULIAN) * ONE_DAY;
String s;
// Test Gregorian computation
cal.setGregorianChange(PURE_GREGORIAN);
cal.clear();
cal.set(year, month, dom);
long calMillis = cal.getTime().getTime();
long delta = calMillis - millis;
cal.setTime(new Date(millis));
year2 = cal.get(YEAR);
month2 = cal.get(MONTH);
dom2 = cal.get(DAY_OF_MONTH);
s = "G " + year + "-" + (month + 1 - JANUARY) + "-" + dom
+ " => " + calMillis
+ " (" + ((float) delta / ONE_DAY) + " day delta) => "
+ year2 + "-" + (month2 + 1 - JANUARY) + "-" + dom2;
if (delta != 0 || year != year2 || month != month2
|| dom != dom2) {
errln(s + " FAIL");
} else {
logln(s);
}
// Test Julian computation
year = DATA[i + 4];
month = DATA[i + 5];
dom = DATA[i + 6];
cal.setGregorianChange(PURE_JULIAN);
cal.clear();
cal.set(year, month, dom);
calMillis = cal.getTime().getTime();
delta = calMillis - millis;
cal.setTime(new Date(millis));
year2 = cal.get(YEAR);
month2 = cal.get(MONTH);
dom2 = cal.get(DAY_OF_MONTH);
s = "J " + year + "-" + (month + 1 - JANUARY) + "-" + dom
+ " => " + calMillis
+ " (" + ((float) delta / ONE_DAY) + " day delta) => "
+ year2 + "-" + (month2 + 1 - JANUARY) + "-" + dom2;
if (delta != 0 || year != year2 || month != month2
|| dom != dom2) {
errln(s + " FAIL");
} else {
logln(s);
}
}
cal.setGregorianChange(new Date(1582 - 1900, OCTOBER, 15));
auxMapping(cal, 1582, OCTOBER, 4);
auxMapping(cal, 1582, OCTOBER, 15);
auxMapping(cal, 1582, OCTOBER, 16);
for (int y = 800; y < 3000; y += 1 + 100 * Math.random()) {
for (int m = JANUARY; m <= DECEMBER; ++m) {
auxMapping(cal, y, m, 15);
}
}
} finally {
TimeZone.setDefault(saveZone);
}
}
private static Date getDate(int year, int month, int date, int hourOfDay, int minute, int second, TimeZone tz) {
GregorianCalendar cal = new GregorianCalendar(tz);
cal.clear();
cal.set(year, month, date, hourOfDay, minute, second);
return cal.getTime();
}