本文整理汇总了Java中java.util.GregorianCalendar.getActualMaximum方法的典型用法代码示例。如果您正苦于以下问题:Java GregorianCalendar.getActualMaximum方法的具体用法?Java GregorianCalendar.getActualMaximum怎么用?Java GregorianCalendar.getActualMaximum使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.GregorianCalendar
的用法示例。
在下文中一共展示了GregorianCalendar.getActualMaximum方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getMonthEndDate
import java.util.GregorianCalendar; //导入方法依赖的package包/类
/**
* 月最后一天
* @param date
* @return
*/
public static Date getMonthEndDate(Date date){
GregorianCalendar c = new GregorianCalendar();
c.setTime(date);
int maxMonth = c.getActualMaximum(Calendar.DAY_OF_MONTH);
c.set(Calendar.DAY_OF_MONTH, maxMonth);
c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 59);
c.set(Calendar.SECOND, 59);
return c.getTime();
}
示例2: Test4166109
import java.util.GregorianCalendar; //导入方法依赖的package包/类
/**
* GregorianCalendar.getActualMaximum() does not account for first day of week.
*/
public void Test4166109() {
/* Test month:
*
* March 1998
* Su Mo Tu We Th Fr Sa
* 1 2 3 4 5 6 7
* 8 9 10 11 12 13 14
* 15 16 17 18 19 20 21
* 22 23 24 25 26 27 28
* 29 30 31
*/
boolean passed = true;
int field = WEEK_OF_MONTH;
GregorianCalendar calendar = new GregorianCalendar(Locale.US);
calendar.set(1998, MARCH, 1);
calendar.setMinimalDaysInFirstWeek(1);
logln("Date: " + calendar.getTime());
int firstInMonth = calendar.get(DAY_OF_MONTH);
for (int firstInWeek = SUNDAY; firstInWeek <= SATURDAY; firstInWeek++) {
calendar.setFirstDayOfWeek(firstInWeek);
int returned = calendar.getActualMaximum(field);
int expected = (31 + ((firstInMonth - firstInWeek + 7) % 7) + 6) / 7;
logln("First day of week = " + firstInWeek
+ " getActualMaximum(WEEK_OF_MONTH) = " + returned
+ " expected = " + expected
+ ((returned == expected) ? " ok" : " FAIL"));
if (returned != expected) {
passed = false;
}
}
if (!passed) {
errln("Test failed");
}
}
示例3: Test4167060
import java.util.GregorianCalendar; //导入方法依赖的package包/类
/**
* Calendar.getActualMaximum(YEAR) works wrong.
*
* Note: Before 1.5, this test case assumed that
* setGregorianChange didn't change object's date. But it was
* changed. See 4928615.
*/
public void Test4167060() {
int field = YEAR;
DateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy G",
Locale.US);
int[][] dates = {
// year, month, day of month
{100, NOVEMBER, 1},
{-99 /*100BC*/, JANUARY, 1},
{1996, FEBRUARY, 29}};
String[] id = {"Hybrid", "Gregorian", "Julian"};
for (int k = 0; k < 3; ++k) {
logln("--- " + id[k] + " ---");
for (int j = 0; j < dates.length; ++j) {
GregorianCalendar calendar = new GregorianCalendar();
if (k == 1) {
calendar.setGregorianChange(new Date(Long.MIN_VALUE));
} else if (k == 2) {
calendar.setGregorianChange(new Date(Long.MAX_VALUE));
}
calendar.set(dates[j][0], dates[j][1], dates[j][2]);
format.setCalendar((Calendar) calendar.clone());
Date dateBefore = calendar.getTime();
int maxYear = calendar.getActualMaximum(field);
logln("maxYear: " + maxYear + " for " + format.format(calendar.getTime()));
logln("date before: " + format.format(dateBefore));
int[] years = {2000, maxYear - 1, maxYear, maxYear + 1};
for (int i = 0; i < years.length; i++) {
boolean valid = years[i] <= maxYear;
calendar.set(field, years[i]);
Date dateAfter = calendar.getTime();
int newYear = calendar.get(field);
calendar.setTime(dateBefore); // restore calendar for next use
logln(" Year " + years[i] + (valid ? " ok " : " bad")
+ " => " + format.format(dateAfter));
if (valid && newYear != years[i]) {
errln(" FAIL: " + newYear + " should be valid; date, month and time shouldn't change");
} else if (!valid && newYear == years[i]) {
errln(" FAIL: " + newYear + " should be invalid");
}
}
}
}
}