Java Code Examples for java.util.Calendar#getActualMaximum()

The following examples show how to use java.util.Calendar#getActualMaximum() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Recurring.java    From material with Apache License 2.0 6 votes vote down vote up
/**
 * Get the day in month of the current month of Calendar.
 * @param cal
 * @param dayOfWeek The day of week.
 * @param orderNum The order number, 0 mean the first, -1 mean the last.
 * @return The day int month
 */
private static int getDay(Calendar cal, int dayOfWeek, int orderNum){
    int day = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

    cal.set(Calendar.DAY_OF_MONTH, day);
    int lastWeekday = cal.get(Calendar.DAY_OF_WEEK);
    int shift = lastWeekday >= dayOfWeek ? (lastWeekday - dayOfWeek) : (lastWeekday + 7 - dayOfWeek);

    //find last dayOfWeek of this month
    day -= shift;

    if(orderNum < 0)
        return day;

    cal.set(Calendar.DAY_OF_MONTH, day);
    int lastOrderNum = (cal.get(Calendar.DAY_OF_MONTH) - 1) / 7;

    if(orderNum >= lastOrderNum)
        return day;

    return day - (lastOrderNum - orderNum) * 7;
}
 
Example 2
Source File: Week.java    From RipplePower with Apache License 2.0 6 votes vote down vote up
@Override
public RegularTimer previous() {
	Week result;
	if (this.week != FIRST_WEEK_IN_YEAR) {
		result = new Week(this.week - 1, this.year);
	} else {
		if (this.year > 1900) {
			int yy = this.year - 1;
			Calendar prevYearCalendar = Calendar.getInstance();
			prevYearCalendar.set(yy, Calendar.DECEMBER, 31);
			result = new Week(prevYearCalendar.getActualMaximum(Calendar.WEEK_OF_YEAR), yy);
		} else {
			result = null;
		}
	}
	return result;

}
 
Example 3
Source File: DateUtils.java    From Spring-Blog with Apache License 2.0 6 votes vote down vote up
/**
 * 根据指定年度和月份获取月末日期
 *
 * @param yearMonth
 * @return
 */
public static Date getLastDayOfMonth(String yearMonth) {
    int year = Integer.parseInt(yearMonth.substring(0, 4));
    int month = Integer.parseInt(yearMonth.substring(4, yearMonth.length()));
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR, year);
    calendar.set(Calendar.MONTH, month - 1);
    calendar.set(Calendar.DATE, 1);

    int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

    calendar.set(Calendar.DAY_OF_MONTH, maxDay);
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);

    return calendar.getTime();
}
 
Example 4
Source File: CalendarPeriodCountCalculator.java    From objectlabkit with Apache License 2.0 6 votes vote down vote up
private int calculateConv360EIsda(final Calendar start, final Calendar end) {
    if (start.equals(end)) {
        return 0;
    }
    int diff;
    int dayStart = start.get(Calendar.DAY_OF_MONTH);
    int dayEnd = end.get(Calendar.DAY_OF_MONTH);
    if (start.getActualMaximum(Calendar.DAY_OF_MONTH) == dayStart) {
        dayStart = CalculatorConstants.MONTH_30_DAYS;
    }
    if (end.get(Calendar.MONTH) != Calendar.FEBRUARY && end.getActualMaximum(Calendar.DAY_OF_MONTH) == dayEnd) {
        dayEnd = CalculatorConstants.MONTH_30_DAYS;
    }

    diff = (end.get(Calendar.YEAR) - start.get(Calendar.YEAR)) * CalculatorConstants.YEAR_360 + (end.get(Calendar.MONTH) - start.get(Calendar.MONTH)) * CalculatorConstants.MONTH_30_DAYS
            + dayEnd - dayStart;
    return diff;
}
 
Example 5
Source File: BGHistory.java    From xDrip-Experimental with GNU General Public License v3.0 6 votes vote down vote up
private int daysBetween(Calendar calendar1, Calendar calendar2) {
    Calendar first, second;
    if (calendar1.compareTo(calendar2) > 0) {
        first = calendar2;
        second = calendar1;
    } else {
        first = calendar1;
        second = calendar2;
    }
    int days = second.get(Calendar.DAY_OF_YEAR) - first.get(Calendar.DAY_OF_YEAR);
    Calendar temp = (Calendar) first.clone();
    while (temp.get(Calendar.YEAR) < second.get(Calendar.YEAR)) {
        days = days + temp.getActualMaximum(Calendar.DAY_OF_YEAR);
        temp.add(Calendar.YEAR, 1);
    }
    return days;
}
 
Example 6
Source File: Util.java    From QiQuYingServer with Apache License 2.0 6 votes vote down vote up
/**
 * @Title: getMonthTimesBE
 * @Description: 获取一月的起止时间
 * @param @param offset 月份偏移量,0为本月,-1为上月,1为下月,如此类推
 * @param @return date[0]:开始时间,格式2012-03-01
 *        00:00:00;date[1]:结束时间,格式2012-03-31 23:59:59;异常为null
 * @return Date[]
 */
public static Date[] getMonthTimesBE(int offset) {
	try {
		Date[] dates = new Date[2];
		// 得到当前日期
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.MONTH, offset);

		int MaxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		// 按你的要求设置时间
		cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), MaxDay,
				23, 59, 59);
		Date end = cal.getTime();
		cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), 1, 00, 00,
				00);
		Date begin = cal.getTime();
		dates[0] = begin;
		dates[1] = end;
		return dates;
	} catch (Exception e) {
		return null;
	}
}
 
Example 7
Source File: DateUtil.java    From quickhybrid-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static int getDaysOfYM(int year, int month) {
    Calendar time = Calendar.getInstance();
    time.clear();
    time.set(Calendar.YEAR, year);
    time.set(Calendar.MONTH, month - 1);
    int day = time.getActualMaximum(Calendar.DAY_OF_MONTH);
    return day;
}
 
Example 8
Source File: DateUtil.java    From AndroidBasicProject with MIT License 5 votes vote down vote up
/**
 * 根据月份获得最大天数
 * @param year 年
 * @param month 月
 * @return 最大天数
 */
public static int getMaxDayByMonth(int year,int month){
    Calendar time=Calendar.getInstance();//使用默认时区和语言环境获得一个日历
    //注意:在使用set方法之前,必须先调用clear(),否则很多信息会继承自系统当前的时间
    time.clear();
    time.set(Calendar.YEAR,year);
    time.set(Calendar.MONTH,month);//注意Calendar对象默认一月是为零的
    int day=time.getActualMaximum(Calendar.DAY_OF_MONTH);//获得本月份的天数
    return day;
}
 
Example 9
Source File: LunarUtil.java    From CalendarView with Apache License 2.0 5 votes vote down vote up
/**
 * 计算两个阳历日期相差的天数。
 *
 * @param startDate 开始时间
 * @param endDate   截至时间
 * @return 天数
 */
private static int daysBetween(Date startDate, Date endDate) {
    int days = 0;
    //将转换的两个时间对象转换成Calendar对象
    Calendar can1 = Calendar.getInstance();
    can1.setTime(startDate);
    Calendar can2 = Calendar.getInstance();
    can2.setTime(endDate);
    //拿出两个年份
    int year1 = can1.get(Calendar.YEAR);
    int year2 = can2.get(Calendar.YEAR);
    //天数

    Calendar can = null;
    //如果can1 < can2
    //减去小的时间在这一年已经过了的天数
    //加上大的时间已过的天数
    if (can1.before(can2)) {
        days -= can1.get(Calendar.DAY_OF_YEAR);
        days += can2.get(Calendar.DAY_OF_YEAR);
        can = can1;
    } else {
        days -= can2.get(Calendar.DAY_OF_YEAR);
        days += can1.get(Calendar.DAY_OF_YEAR);
        can = can2;
    }
    for (int i = 0; i < Math.abs(year2 - year1); i++) {
        //获取小的时间当前年的总天数
        days += can.getActualMaximum(Calendar.DAY_OF_YEAR);
        //再计算下一年。
        can.add(Calendar.YEAR, 1);
    }
    return days;
}
 
Example 10
Source File: YearMonthDayActivity.java    From BigApp_Discuz_Android with Apache License 2.0 5 votes vote down vote up
/**
 * Updates day wheel. Sets max days according to selected month and year
 */
void updateDays(WheelView year, WheelView month, WheelView day) {
	Calendar calendar = Calendar.getInstance();
	calendar.set(Calendar.YEAR,
			calendar.get(Calendar.YEAR) + year.getCurrentItem());
	calendar.set(Calendar.MONTH, month.getCurrentItem());

	int maxDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
	day.setViewAdapter(new DateNumericAdapter(this, 1, maxDays, calendar
			.get(Calendar.DAY_OF_MONTH) - 1));
	curDay = Math.min(maxDays, day.getCurrentItem() + 1);
	day.setCurrentItem(curDay - 1, true);
}
 
Example 11
Source File: DailySalesHeatMapFacadeREST.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
private HashMap<String, Long> runBaseQuery(Date date){
    Calendar cal = Calendar.getInstance();
    long DIFF, TIME = System.currentTimeMillis(), START_TIME = System.currentTimeMillis();
    Query baseQuery = em.createQuery(BASE_QUERY);
    HashMap<String, Long> result = new HashMap<String, Long>();
    {
        cal.setTime(date);
        int dayMin = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        int dayMax = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, dayMin);
        Parameter<Date> p1 = baseQuery.getParameter("oldStartDate", Date.class);
        baseQuery.setParameter(p1, cal.getTime());
        cal.set(Calendar.DAY_OF_MONTH, dayMax);
        Parameter<Date> p2 = baseQuery.getParameter("oldEndDate", Date.class);
        baseQuery.setParameter(p2, cal.getTime());

        List<Object[]> resultList = baseQuery.getResultList();

        DIFF = (System.currentTimeMillis() - TIME);
        System.out.println("    Q TIME = "+DIFF+"ms");

        for (int i=0; i < resultList.size(); i++){
            Object o[] = resultList.get(i);
            result.put((String)o[1],(Long)o[0]);
        }
    }
    return result;
}
 
Example 12
Source File: PanchangExporter.java    From Astrosoft with GNU General Public License v2.0 5 votes vote down vote up
private void export(Panchang p) throws XMLStreamException {
	
	Calendar cal = p.getDate();
	if (cal.get(Calendar.DATE) == 1){
		xmlWriter.add(xmlef.createStartElement(XmlConsts.MONTH_TAG, null,null));
		xmlWriter.add(xmlef.createAttribute(XmlConsts.Title, "Panchang for " + df.format(p.getDate().getTime())));
	}
	exportTableData(p.getPanchangTableData(),XmlConsts.PANCHANG_INFO_TAG,XmlConsts.INFO_TAG);
	if (cal.get(Calendar.DATE) == cal.getActualMaximum(Calendar.DATE)){
		xmlWriter.add(xmlef.createEndElement(XmlConsts.MONTH_TAG, null));
	}
}
 
Example 13
Source File: CalendarGridPanel.java    From microba with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private int getFocusedIndex() {
	Calendar bc = getCalendar(baseDate);
	Calendar fc = getCalendar(focusDate);
	bc.set(Calendar.DAY_OF_MONTH, 1);
	int skipBefore = bc.get(Calendar.DAY_OF_WEEK) - bc.getFirstDayOfWeek();
	if (skipBefore < 0)
		skipBefore = 7 + skipBefore;
	int selDay = fc.get(Calendar.DAY_OF_MONTH);
	int maxDay = bc.getActualMaximum(Calendar.DAY_OF_MONTH);
	if (selDay > maxDay)
		selDay = maxDay;
	return skipBefore + selDay - 1;
}
 
Example 14
Source File: DateUtil.java    From timecat with Apache License 2.0 4 votes vote down vote up
/**
 * 计算日期差
 * @param date1
 * @param date2
 * @param field
 * 		yyyy:年
 * 		MM:月
 * 		dd:日
 * @return
 * 		date2 - date1
 */
public static int dateDiff(Date date1, Date date2, String field) {
    boolean flag = date1.compareTo(date2) > 0;
    if (flag) {
        Date tmp = date1;
        date1 = date2;
        date2 = tmp;
    }

    Calendar cal1 = Calendar.getInstance();
    cal1.setTime(date1);
    int year1 = cal1.get(Calendar.YEAR);
    int month1 = cal1.get(Calendar.MONTH);
    Calendar cal2 = Calendar.getInstance();
    cal2.setTime(date2);
    int year2 = cal2.get(Calendar.YEAR);
    int month2 = cal2.get(Calendar.MONTH);
    int yearDiff = year2- year1;

    int diff = 0;
    if ("yyyy".equals(field)) {
        diff = yearDiff;
    } else if ("MM".equals(field)) {
        if (yearDiff <= 0) {
            diff = month2 - month1;
        } else if (yearDiff == 1) {
            diff = 12 - month1 + month2;
        } else {
            diff = 12 - month1 + (year2 - year1 - 1) * 12 + month2;
        }
    } else if ("dd".equals(field)) {
        if (yearDiff <= 0) {
            diff = cal2.get(Calendar.DAY_OF_YEAR) - cal1.get(Calendar.DAY_OF_YEAR);
        } else if (yearDiff == 1) {
            diff = getDaysLeftOfYear(date1) + cal2.get(Calendar.DAY_OF_YEAR);
        } else {
            diff = getDaysLeftOfYear(date1);
            Calendar cal;
            for (int i = 1; i < yearDiff; i++) {
                cal = Calendar.getInstance();
                cal.setTime(dateAdd(date1, "yyyy", i));
                diff += cal.getActualMaximum(Calendar.DAY_OF_YEAR);
            }
            diff += cal2.get(Calendar.DAY_OF_YEAR);
        }
    }

    return flag ? -1 * diff : diff;
}
 
Example 15
Source File: FunAlmanac.java    From xDrip-plus with GNU General Public License v3.0 4 votes vote down vote up
public static Reply getRepresentation(double bgValue, String arrowName, boolean usingMgDl) {
    final Calendar c = Calendar.getInstance();
    int currentDayOfWeek;
    boolean preserveDayOfWeek = true; // keep same or represent trend
    c.setTimeInMillis(JoH.tsl());
    if (preserveDayOfWeek) {
        switch (arrowName) {
            case "DoubleDown":
                currentDayOfWeek = Calendar.MONDAY;
                break;
            case "SingleDown":
                currentDayOfWeek = Calendar.TUESDAY;
                break;
            case "FortyFiveDown":
                currentDayOfWeek = Calendar.WEDNESDAY;
                break;
            case "Flat":
                currentDayOfWeek = Calendar.THURSDAY;
                break;
            case "FortyFiveUp":
                currentDayOfWeek = Calendar.FRIDAY;
                break;
            case "SingleUp":
                currentDayOfWeek = Calendar.SATURDAY;
                break;
            case "DoubleUp":
                currentDayOfWeek = Calendar.SUNDAY;
                break;
            default:
                currentDayOfWeek = Calendar.THURSDAY;
        }
    } else currentDayOfWeek = c.get(Calendar.DAY_OF_WEEK);

    int macro = 0, micro = 0;
    double value = bgValue;
    if (usingMgDl) {
        if (value > 299) value = 299;
        else if (value < 10) value = 10;
        macro = (int) value / 10;
        micro = (int) value % 10;
    } else {
        value = roundDouble(mmolConvert(value), 1);
        if (value >= 18.9) value = 18.9;
        macro = (int) value;
        micro = (int) (JoH.roundDouble(value - macro, 1) * 10);
        macro++;
    }
    if (micro == 0) micro = 10; //10th month will be displayed as 0 on the custom watchface
    micro--;
    c.set(Calendar.DAY_OF_MONTH, macro); //day 1 represent 0
    c.set(Calendar.MONTH, micro);
    int max = c.getActualMaximum(Calendar.DAY_OF_MONTH);
    int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
    while ((dayOfWeek != currentDayOfWeek) || ((max < 29))) {
        c.set(Calendar.YEAR, c.get(Calendar.YEAR) + 1);
        c.set(Calendar.DAY_OF_MONTH, macro);
        c.set(Calendar.MONTH, micro);
        max = c.getActualMaximum(Calendar.DAY_OF_MONTH);
        dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
    }
    String textVal = Double.toString(value) + " " + Unitized.unit(usingMgDl) + ", " + arrowName;
    return new Reply(c.getTimeInMillis(), textVal);
}
 
Example 16
Source File: DateSelectionDialog.java    From APICloud-Studio with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Sets the days to be displayed. The last days of the
 * previous month are included, just like the first days 
 * of the next month.
 * 
 * The size of a DaySelectionCanvas is assumed to be 6 x 7 (rows x columns).
 */
private void refreshDate() {
	refreshing = true;
	Calendar calendar = DateFormat.getDateTimeInstance().getCalendar();
	calendar.setTime(date);
	int day = calendar.get(Calendar.DAY_OF_MONTH);
	yearSpinner.setSelection(calendar.get(Calendar.YEAR));
	monthCombo.select(calendar.get(Calendar.MONTH));

	int maxDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
	calendar.add(Calendar.MONTH, -1);
	int prevMaxDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
	calendar.add(Calendar.MONTH, 1);
	
	calendar.set(Calendar.DAY_OF_MONTH, 1);
	int weekDay = calendar.get(Calendar.DAY_OF_WEEK);
	int convertedWeekDay = (weekDay + 5) % 7;
	boolean addFirstWeek = false;
	int remainingDays = (42 - maxDays) - convertedWeekDay;
	if ((remainingDays - convertedWeekDay) > 7) {
		addFirstWeek = true;
	}
	int[] days = new int[42];
	int i=0;
	int numPrefix = convertedWeekDay; 
	if (addFirstWeek) {
		numPrefix += 7;
	}
	for (; i < numPrefix; i++) {
		days[i] = prevMaxDays - numPrefix + i + 1;
	}
	int numMonth = numPrefix + maxDays;
	for (; i < numMonth; i++) {
		days[i] = i - numPrefix + 1;
	}
	for (; i < days.length; i++) {
		days[i] = i - numMonth + 1;
	}
	
	daysComp.setDays(days);
	daysComp.setSelectedDay(day);
	refreshing = false;
}
 
Example 17
Source File: OkrWorkBaseInfoQueryService.java    From o2oa with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * TODO:根据周期方式,以后周期时间点,和开始时间来计算下一个周期时间点
 * 
 * @param reportCycle
 * @param reportDayInCycle
 * @param date
 * @return
 * @throws Exception
 */
private Date calculateNextCycleTime(String reportStartTime, String reportCycle, Integer reportDayInCycle,
		Date lastReportDate) throws Exception {
	int reportDay = 0;
	int dayMaxNumber = 0;
	Calendar calendar = Calendar.getInstance();
	calendar.setTime(dateOperation.getDateFromString(
			dateOperation.getDateStringFromDate(lastReportDate, "yyyy-MM-dd") + " " + reportStartTime));
	if (reportCycle != null && reportCycle.trim().equals("每月汇报")) {
		dayMaxNumber = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		if (dayMaxNumber < reportDayInCycle) {
			reportDay = dayMaxNumber;
		} else {
			reportDay = reportDayInCycle;
		}
		calendar.set(Calendar.DAY_OF_MONTH, reportDay);
		// 如果本月汇报时间已经过了,那么下月再汇报
		System.out.println(calendar.getTime() + ".before(" + lastReportDate + "):"
				+ calendar.getTime().before(lastReportDate));
		while (calendar.getTime().before(lastReportDate)) {
			calendar.set(Calendar.DAY_OF_MONTH, reportDay);
			calendar.add(Calendar.MONTH, 1);
		}
	} else if (reportCycle != null && reportCycle.trim().equals("每周汇报")) {
		dayMaxNumber = 7;
		if (dayMaxNumber < reportDayInCycle) {
			reportDay = dayMaxNumber;
		} else {
			reportDay = reportDayInCycle;
		}
		calendar.set(Calendar.DAY_OF_WEEK, reportDay);
		// 如果本周汇报时间已经过了,那么下周再汇报
		System.out.println(calendar.getTime() + ".before(" + lastReportDate + "):"
				+ calendar.getTime().before(lastReportDate));
		while (calendar.getTime().before(lastReportDate)) {
			calendar.set(Calendar.DAY_OF_WEEK, reportDay);
			calendar.add(Calendar.WEEK_OF_YEAR, 1);
		}
	}
	// 判断是否周末
	while (dateOperation.isWeekend(calendar.getTime())) {
		calendar.add(Calendar.DATE, 1);
	}
	return dateOperation.getDateFromString(
			dateOperation.getDateStringFromDate(calendar.getTime(), "yyyy-MM-dd") + " " + reportStartTime);
}
 
Example 18
Source File: WeekPicker.java    From 365browser with Apache License 2.0 4 votes vote down vote up
private int getNumberOfWeeks(int year) {
    // Create a date in the middle of the year, where the week year matches the year.
    Calendar date = createDateFromWeek(year, 20);
    return date.getActualMaximum(Calendar.WEEK_OF_YEAR);
}
 
Example 19
Source File: SingleDateAndTimePicker.java    From SingleDateAndTimePicker with Apache License 2.0 4 votes vote down vote up
private void updateDaysOfMonth(@NonNull Calendar calendar) {
    int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    daysOfMonthPicker.setDaysInMonth(daysInMonth);
    daysOfMonthPicker.updateAdapter();
}
 
Example 20
Source File: CalendarUtil.java    From Phonograph with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Gets the number of days for the given month in the given year.
 *
 * @param year  The year.
 * @param month The month (1 - 12).
 * @return The days in that month/year.
 */
private int getDaysInMonth(int year, int month) {
    final Calendar monthCal = new GregorianCalendar(calendar.get(Calendar.YEAR), month, 1);
    return monthCal.getActualMaximum(Calendar.DAY_OF_MONTH);
}