List of usage examples for java.util Calendar getActualMaximum
public int getActualMaximum(int field)
Calendar
. From source file:org.kuali.coeus.common.budget.framework.personnel.BudgetPersonnelDetails.java
public ScaleTwoDecimal getPersonMonths() { ScaleTwoDecimal result = null;//from w ww. j a v a 2s . c o m if (getStartDate() != null && getEndDate() != null) { Calendar startDateCalendar = Calendar.getInstance(); startDateCalendar.setTime(getStartDate()); int startMonth = startDateCalendar.get(MONTH); Calendar endDateCalendar = Calendar.getInstance(); endDateCalendar.setTime(getEndDate()); double personMonths = 0d; while (startDateCalendar.compareTo(endDateCalendar) <= 0) { double noOfDaysInMonth = startDateCalendar.getActualMaximum(DAY_OF_MONTH); double noOfActualDays = 0; if (startDateCalendar.get(MONTH) == endDateCalendar.get(MONTH) && startDateCalendar.get(Calendar.YEAR) == endDateCalendar.get(Calendar.YEAR)) { noOfActualDays = endDateCalendar.get(DAY_OF_MONTH) - startDateCalendar.get(DAY_OF_MONTH) + 1; } else if (startDateCalendar.get(MONTH) == startMonth) { noOfActualDays = noOfDaysInMonth - startDateCalendar.get(DAY_OF_MONTH) + 1; } else { noOfActualDays = noOfDaysInMonth; } startDateCalendar.add(MONTH, 1); startDateCalendar.set(DAY_OF_MONTH, 1); personMonths += (noOfActualDays / noOfDaysInMonth); } result = new ScaleTwoDecimal(new BigDecimal(personMonths).setScale(2, RoundingMode.HALF_UP) .multiply(getPercentEffort().bigDecimalValue()) .divide(new ScaleTwoDecimal(100).bigDecimalValue(), RoundingMode.HALF_UP)); } return result; }
From source file:no.abmu.abmstatistikk.annualstatistic.service.AnswerHelper.java
private String validateYearDaysCommon() { String valueString = answer.getValue(); if (valueString == null || valueString.equals("")) { return null; /* Allow for no input */ }//from www . j a v a 2s . co m /* Find year of schema and use calendar to find number of days of * that year. */ Report report = answer.getReport(); if (report == null) { logger.error("Answer not bound to a report: " + answer); return "No report for answer! This should never happen."; } Schema schema = report.getSchema(); if (schema == null) { logger.error("Report not bound to a schema: " + report); return "No schema for report! This should never happen."; } SchemaHelper schemaHelper = new SchemaHelper(schema); int year = schemaHelper.getYear(); Calendar calendar = new GregorianCalendar(year, 1, 1); int daysOfYyear = calendar.getActualMaximum(Calendar.DAY_OF_YEAR); valueString = valueString.replace(',', '.'); float value = Float.parseFloat(valueString); if (value > daysOfYyear || value < 0) { return "schema.error.invalid_number_of_days_pr_year"; } return null; }
From source file:no.abmu.abmstatistikk.annualstatistic.service.AnswerHelper.java
/** * validateYearHours./* w w w. ja v a 2 s .c o m*/ * * @return */ private String validateYearHours() { String error = validateDigit(0); if (error != null) { return error; } String valueString = answer.getValue(); if (valueString == null || valueString.equals("")) { return null; /* Allow for no input */ } /* Find year of schema and use calendar to find number of days of * that year. */ Report report = answer.getReport(); if (report == null) { logger.error("Answer not bound to a report: " + answer); return "No report for answer! This should never happen."; } Schema schema = report.getSchema(); if (schema == null) { logger.error("Report not bound to a schema: " + report); return "No schema for report! This should never happen."; } SchemaHelper schemaHelper = new SchemaHelper(schema); int year = schemaHelper.getYear(); Calendar calendar = new GregorianCalendar(year, 1, 1); int daysOfYear = calendar.getActualMaximum(Calendar.DAY_OF_YEAR); int value = Integer.parseInt(valueString); if (value > daysOfYear * 24 || value < 0) { return "schema.error.invalid_number_of_hours_pr_year"; } return null; }
From source file:org.pentaho.di.core.row.ValueDataUtil.java
public static Object addMonths(ValueMetaInterface metaA, Object dataA, ValueMetaInterface metaB, Object dataB) throws KettleValueException { if (dataA != null && dataB != null) { Calendar cal = Calendar.getInstance(); cal.setTime(metaA.getDate(dataA)); int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH); int day = cal.get(Calendar.DAY_OF_MONTH); month += metaB.getInteger(dataB).intValue(); int newyear = year + (int) Math.floor(month / 12); int newmonth = month % 12; cal.set(newyear, newmonth, 1);/*from w w w . j av a2s .com*/ int newday = cal.getActualMaximum(Calendar.DAY_OF_MONTH); if (newday < day) { cal.set(Calendar.DAY_OF_MONTH, newday); } else { cal.set(Calendar.DAY_OF_MONTH, day); } return (cal.getTime()); } else { throw new KettleValueException("Unable to add months with a null value"); } }
From source file:stg.utils.immutable.Day.java
/** * Returns the maximum value for the given field. * //from w w w . j ava2 s . c o m * @param field * Calendar values for fields. * @return int maximum value for the given field. */ public int getMaximum(int field) { Calendar calendar = getCalendarInstance(tzone); return calendar.getActualMaximum(field); }
From source file:org.kuali.mobility.events.controllers.CalendarController.java
@RequestMapping(value = "/month", method = RequestMethod.GET) public String month(HttpServletRequest request, Model uiModel, @RequestParam(required = false) String date) { User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); SimpleDateFormat my = new SimpleDateFormat("yyyyMM"); Calendar selectedDate = Calendar.getInstance(); try {/* ww w. ja v a 2 s . co m*/ if (date != null) { selectedDate.setTime(my.parse(date)); } } catch (ParseException e) { } try { Filter filter = (Filter) request.getSession().getAttribute("calendar.event.filterId"); MonthViewEvents monthEvents = calendarEventOAuthService.retrieveMonthEvents(user.getUserId(), selectedDate.getTime(), filter != null ? filter.getFilterId() : null); uiModel.addAttribute("viewData", monthEvents.getViewData()); uiModel.addAttribute("appData", monthEvents.getAppData()); int days = selectedDate.getActualMaximum(Calendar.DATE); Calendar startDate = (Calendar) selectedDate.clone(); startDate.set(Calendar.DATE, selectedDate.getActualMinimum(Calendar.DATE)); days += startDate.get(Calendar.DAY_OF_WEEK) - 1; Calendar endDate = (Calendar) selectedDate.clone(); endDate.set(Calendar.DATE, selectedDate.getActualMaximum(Calendar.DATE)); days += 7 - endDate.get(Calendar.DAY_OF_WEEK); startDate.set(Calendar.DAY_OF_WEEK, 1); Map<String, MobileDayOfMonth> daysInMonth = new LinkedHashMap<String, MobileDayOfMonth>(); uiModel.addAttribute("selectedDate", sdf.format(selectedDate.getTime())); for (int i = 0; i < days; i++) { MobileDayOfMonth mobileDayOfMonth = new MobileDayOfMonth(startDate.get(Calendar.DATE)); mobileDayOfMonth.setCurrentMonth(startDate.get(Calendar.MONTH) == selectedDate.get(Calendar.MONTH)); if (!mobileDayOfMonth.isCurrentMonth()) { mobileDayOfMonth.setBeforeCurrentMonth( startDate.get(Calendar.MONTH) < selectedDate.get(Calendar.MONTH)); } mobileDayOfMonth.setMonthYear(my.format(startDate.getTime())); mobileDayOfMonth.setDayOfWeek(startDate.get(Calendar.DAY_OF_WEEK)); daysInMonth.put(sdf.format(startDate.getTime()), mobileDayOfMonth); startDate.add(Calendar.DATE, 1); } for (Iterator iterator = monthEvents.getEvents().entrySet().iterator(); iterator.hasNext();) { Map.Entry<String, List<CalendarViewEvent>> entry = (Map.Entry<String, List<CalendarViewEvent>>) iterator .next(); MobileDayOfMonth dayInMonth = daysInMonth.get(entry.getKey()); dayInMonth.setHasEvents(true); dayInMonth.setEvents(entry.getValue()); } uiModel.addAttribute("events", daysInMonth); Calendar previousCalendar = (Calendar) selectedDate.clone(); previousCalendar.set(Calendar.DATE, 1); previousCalendar.getTime(); previousCalendar.add(Calendar.MONTH, -1); Calendar nextCalendar = (Calendar) selectedDate.clone(); nextCalendar.set(Calendar.DATE, 1); nextCalendar.getTime(); nextCalendar.add(Calendar.MONTH, 1); uiModel.addAttribute("previousMonth", my.format(previousCalendar.getTime())); uiModel.addAttribute("nextMonth", my.format(nextCalendar.getTime())); uiModel.addAttribute("monthYear", my.format(selectedDate.getTime())); uiModel.addAttribute("today", sdf.format(new Date())); uiModel.addAttribute("filter", filter); } catch (PageLevelException pageLevelException) { uiModel.addAttribute("message", pageLevelException.getMessage()); return "calendar/message"; } return "calendar/month"; }
From source file:so.contacts.hub.basefunction.account.ui.YellowpagePersonalInfoActivity.java
/** * ?????????(2829)/* ww w. j av a 2 s . c om*/ * * @param mYearWheelView2 * @param mMonthWheelView2 * @param mDayWheelView2 void */ private void updateDays(WheelView year, WheelView month, WheelView day) { // Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.YEAR, year.getCurrentItem()); calendar.set(Calendar.MONTH, month.getCurrentItem()); mMaxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); mDayAdapter = new WheelNumericAdapter(this, 1, mMaxDay, getString(R.string.putao_personal_data_day)); mDayWheelView.setViewAdapter(mDayAdapter); }
From source file:picocash.Picocash.java
private List<Transaction> getTransactionsForSelectedMonth(Account account, int year, int month) { log.debug("selected [" + account + "] on [" + year + "/" + month + "]"); Calendar date = Calendar.getInstance(); date.setLenient(false);/*from w ww . j av a2 s. com*/ date.set(Calendar.YEAR, year); date.set(Calendar.MONTH, month); date.set(Calendar.DAY_OF_MONTH, date.getActualMinimum(Calendar.DAY_OF_MONTH)); date.set(Calendar.HOUR_OF_DAY, date.getActualMinimum(Calendar.HOUR_OF_DAY)); date.set(Calendar.MINUTE, date.getActualMinimum(Calendar.MINUTE)); date.set(Calendar.SECOND, date.getActualMinimum(Calendar.SECOND)); date.set(Calendar.MILLISECOND, date.getActualMinimum(Calendar.MILLISECOND)); long startDate = date.getTimeInMillis(); if (log.isTraceEnabled()) { log.trace("startDate [" + date.getTime() + "]"); } date.set(Calendar.DAY_OF_MONTH, date.getActualMaximum(Calendar.DAY_OF_MONTH)); date.set(Calendar.HOUR_OF_DAY, date.getActualMaximum(Calendar.HOUR_OF_DAY)); date.set(Calendar.MINUTE, date.getActualMaximum(Calendar.MINUTE)); date.set(Calendar.SECOND, date.getActualMaximum(Calendar.SECOND)); date.set(Calendar.MILLISECOND, date.getActualMaximum(Calendar.MILLISECOND)); long endDate = date.getTimeInMillis(); if (log.isTraceEnabled()) { log.trace("endDate [" + date.getTime() + "]"); } final List<Transaction> transactions = Services.getSelectedPersistenceMan() .getAllTransactionsForAccount(account, startDate, endDate); if (log.isTraceEnabled()) { final List<Transaction> allTransactionsForAccount = Services.getSelectedPersistenceMan() .getAllTransactionsForAccount(account); for (Transaction transaction : allTransactionsForAccount) { final long transactionDate = transaction.getTransactionDate(); Calendar test = Calendar.getInstance(); test.setTimeInMillis(transactionDate); log.trace(test.getTime()); } log.trace("current available transactions [" + transactions.size() + "]"); } return transactions; }
From source file:velo.ejb.seam.action.HomeActionsBean.java
public String getArray() { log.debug("Get User creation per month for last year method has been invoked!"); try {/*ww w.ja v a 2 s. c o m*/ Writer writer1 = new java.io.StringWriter(); XmlWriter xmlwriter1 = new SimpleXmlWriter(writer1); PrettyPrinterXmlWriter pxr = new PrettyPrinterXmlWriter(xmlwriter1); pxr.setIndent("\t"); pxr.setNewline("\n"); pxr.writeXmlVersion("1.0", null, "yes").writeEntity("items"); Calendar c = Calendar.getInstance(); String[] monthName = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; //move the calendar to 12 months back c.add(Calendar.MONTH, -11); //build dates(1st day) of the last 12 months List<Date> yearlyMonths = new ArrayList<Date>(); log.debug("Building months(1 year) before current month..."); for (int i = 0; i <= 11; i++) { log.trace("Adding monthly date obj: #0", c.getTime()); yearlyMonths.add(c.getTime()); c.add(Calendar.MONTH, 1); } //iterate over months, get the amount of users created per month for (Date currFirstDayOfMonth : yearlyMonths) { c.setTime(currFirstDayOfMonth); c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH)); Long amount = userManager.getCreatedUsersAmount(currFirstDayOfMonth, c.getTime()); pxr.writeEntity("item").writeAttribute("month", monthName[c.get(Calendar.MONTH)]) .writeAttribute("year", c.get(Calendar.YEAR)).writeAttribute("value", amount).endEntity(); } pxr.endEntity(); log.debug("Dump of xml: " + writer1.toString()); return writer1.toString(); } catch (IOException e) { e.printStackTrace(); return null; } /* .writeEntity("item").writeAttribute("month", "Jan").writeAttribute("value", 10).endEntity() .writeEntity("item").writeAttribute("month", "Feb").writeAttribute("value", 20).endEntity() .writeEntity("item").writeAttribute("month", "Mar").writeAttribute("value", 30).endEntity() .writeEntity("item").writeAttribute("month", "Apr").writeAttribute("value", 40).endEntity() .writeEntity("item").writeAttribute("month", "Jun").writeAttribute("value", 50).endEntity() .writeEntity("item").writeAttribute("month", "Jul").writeAttribute("value", 60).endEntity() .writeEntity("item").writeAttribute("month", "Aug").writeAttribute("value", 70).endEntity() */ }
From source file:stg.utils.immutable.Day.java
/** * Date Arithmetic function. Adds the specified (signed) amount of time to * the given time field, based on the calendar's rules. * //from ww w .j a v a 2 s . com * A fixed date is that if a schedule is set on Feb 28 and it is to be * advanced by one month then next month date will be Mar 28 and not Mar 31. * So 28 is the date that is fixed. This is applicable to advance method for * fields like {@link Calendar#MONTH} and {@link Calendar#YEAR}. * * @param field * Calendar fields {@link Calendar}. * @param amount * the amount of date or time to be added to the field. * @exception IllegalArgumentException * if an unknown field is given. */ public Day advance(int field, int amount, boolean fixedDate) { int newYear = year; int newMonth = month; int newDay = day; int newHour = hour; int newMinutes = minutes; int newSeconds = seconds; int newMilliseconds = milliseconds; Calendar calendar = getCalendar(); boolean isOnLastDate = (day == calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); while (true) { // This is to incorporate the FIXED DATE. // In FIXED DATE, the date (day) is important and it should not // change in accordance to the month. // E.g. Fixed on every 30th of the month then Feb must be skipped as // Feb does not have 30th. calendar.add(field, amount); int nyear = calendar.get(Calendar.YEAR); int nmonth = calendar.get(Calendar.MONTH) + 1; int nday = calendar.get(Calendar.DAY_OF_MONTH); int nhour = calendar.get(Calendar.HOUR_OF_DAY); int nminutes = calendar.get(Calendar.MINUTE); int nseconds = calendar.get(Calendar.SECOND); int mseconds = calendar.get(Calendar.MILLISECOND); if (fixedDate && (field == Calendar.MONTH || field == Calendar.YEAR)) { // This // is // only // for // Month // & // Year try { // Parse the date with the old day and rest all the new // fields from the advanced date. // Generates an exception means that the respective month // does not have that day then advance again // till the parse does not throw an exception. Day.createDay(nyear, nmonth, day, nhour, nminutes, nseconds, mseconds); newYear = nyear; newMonth = nmonth; // day is not set as the day must be constant for Fixed // Date. newHour = nhour; newMinutes = nminutes; newSeconds = nseconds; newMilliseconds = mseconds; break; } catch (Throwable e) { // do nothing. } } else { newYear = nyear; newMonth = nmonth; newDay = nday; newHour = nhour; newMinutes = nminutes; newSeconds = nseconds; newMilliseconds = mseconds; break; } } // end while if (!fixedDate) { if (isOnLastDate) { if (field == Calendar.MONTH || field == Calendar.YEAR) { if (day != calendar.getActualMaximum(Calendar.DAY_OF_MONTH)) { newDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); } } } // is on last date } // if not fixed date return Day.createDay(newYear, newMonth, newDay, newHour, newMinutes, newSeconds, newMilliseconds, tzone); }