List of usage examples for java.util Calendar getActualMaximum
public int getActualMaximum(int field)
Calendar
. From source file:byku.traindroid.DataFacade.java
public static String UpdateTimeTable(Station from, Station to, int dayFrom, int daysCount, Boolean updateYandex, Boolean updateTutu) {/*from ww w . j a v a 2s .c o m*/ String result = ""; Calendar calendar = Calendar.getInstance(); int day = calendar.get(Calendar.DATE) + dayFrom; int month = calendar.get(Calendar.MONTH) + 1; int year = calendar.get(Calendar.YEAR); for (int i = 0; i < daysCount; ++i) { // Yes, it should be something like date.addDays(1) but it already tested and I'm lazy. if (day > calendar.getActualMaximum(Calendar.DAY_OF_MONTH)) { day -= calendar.getActualMaximum(Calendar.DAY_OF_MONTH); ++month; if (month > 12) { month = 1; ++year; } } String date = Utils.DateToString(day, month, year); if (updateTutu) { String url = String.format(TUTU_ADRESS, from.getTutuId(), to.getTutuId(), date); String message = processSource(url, from, to, date, TUTU_REGEXP, "tutu"); result += (message != "") ? "\n" + message : ""; } if (updateYandex) { String fromType = getYandexStationType(from.getYandexId()); String toType = getYandexStationType(to.getYandexId()); String url = String.format(YANDEX_ADRESS, fromType, from.getYandexId(), toType, to.getYandexId(), date); String message = processSource(url, from, to, date, YANDEX_REGEXP, "yandex"); result += (message != "") ? "\n" + message : ""; } ++day; } return result; }
From source file:controller.InputSanitizer.java
/** * Ueberprueft ob ein Datum day.month.year ein gueltiges datum ergibt * @param day//from w w w . jav a 2s .co m * @param month * @param year * @return Date.getTime () if valid * else error message */ public static String checkDate(String day, String month, String year) { String result = null; DateFormat formatter = new SimpleDateFormat("dd.MM.yyyy"); Calendar cal = new GregorianCalendar(); Date projectStartDate = null; String projectStart = day + "." + month + "." + year; int monthInt = Integer.parseInt(month) - 1; int dayInt = Integer.parseInt(day); cal.set(Calendar.MONTH, monthInt); int maxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH); if (dayInt <= maxDay) { try { formatter.setLenient(false); projectStartDate = formatter.parse(projectStart); result = Long.toString(projectStartDate.getTime()); } catch (ParseException e) { result = "Ihre Eingabe ist kein gültiges Datum."; } } else { result = "Ihre Eingabe ist kein gültiges Datum."; } return result; }
From source file:org.openmrs.module.hospitalcore.util.PatientUtils.java
/** * Estimate age using birthdate//w ww. j a va 2 s .c om * * @param birthdate * @return * @throws ParseException */ public static String estimateAge(Date date) { String age = "~"; // new date Calendar cal = Calendar.getInstance(); // set to old date Calendar cal2 = Calendar.getInstance(); cal2.setTime(date); Date date2 = cal.getTime(); int yearNew = cal.get(Calendar.YEAR); int yearOld = cal2.get(Calendar.YEAR); int monthNew = cal.get(Calendar.MONTH); int monthOld = cal2.get(Calendar.MONTH); int dayNew = cal.get(Calendar.DAY_OF_MONTH); int dayOld = cal2.get(Calendar.DAY_OF_MONTH); int maxDayInOldMonth = cal2.getActualMaximum(Calendar.DAY_OF_MONTH); int yearDiff = yearNew - yearOld; int monthDiff = monthNew - monthOld; int dayDiff = dayNew - dayOld; int ageYear = yearDiff, ageMonth = monthDiff, ageDay = dayDiff; if (monthDiff < 0) { ageYear--; ageMonth = 12 - Math.abs(monthDiff); } if (dayDiff < 0) { ageMonth--; if (ageMonth < 0) { ageYear--; ageMonth = 12 - Math.abs(ageMonth); } ageDay = maxDayInOldMonth - dayOld + dayNew; } if (ageYear >= 1) { age += ageYear; if (ageMonth >= 6) { age += ".5"; } if (ageYear == 1) { age += " year"; } else { age += " years"; } } else if (ageYear <= 0) { if (ageMonth >= 1) { if (ageMonth == 1) { age += ageMonth + " month "; } else { age += ageMonth + " months "; } } if (ageMonth <= 0) { if ((ageDay == 1) || (ageDay == 0)) { age += ageDay + " day "; } else { age += ageDay + " days "; } } } return age; }
From source file:org.energy_home.jemma.internal.ah.eh.esp.ESPContainersDataUtils.java
public static long getNormalizedEndTime(Calendar c, long endTime, int resolution) { c.setTimeInMillis(endTime);// ww w .j a v a2 s . c om c.set(Calendar.MILLISECOND, 999); switch (resolution) { case ESPService.YEAR_RESOLUTION: c.set(Calendar.MONTH, 11); case ESPService.MONTH_RESOLUTION: c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH)); case ESPService.DAY_RESOLUTION: c.set(Calendar.HOUR_OF_DAY, 23); case ESPService.HOUR_RESOLUTION: c.set(Calendar.MINUTE, 59); c.set(Calendar.SECOND, 59); } long time = c.getTimeInMillis(); return time; }
From source file:org.apache.lens.cube.metadata.DateUtil.java
public static CoveringInfo getWeeklyCoveringInfo(Date from, Date to) { int dayDiff = 0; Date tmpFrom = from;//from w w w . j a va 2 s . c om while (tmpFrom.before(to)) { tmpFrom = DateUtils.addDays(tmpFrom, 1); dayDiff++; } if (dayDiff < 7) { return new CoveringInfo(0, false); } Calendar cal = Calendar.getInstance(); cal.setTime(from); int fromDay = cal.get(Calendar.DAY_OF_WEEK); cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY); Date fromWeekStartDate = cal.getTime(); boolean coverable = dayDiff % 7 == 0; if (fromWeekStartDate.before(from)) { // Count from the start of next week dayDiff -= (cal.getActualMaximum(Calendar.DAY_OF_WEEK) - (fromDay - Calendar.SUNDAY)); coverable = false; } return new CoveringInfo(dayDiff / 7, coverable); }
From source file:org.dspace.app.statistics.CreateStatReport.java
/** * This script starts from the year and month specified below and loops each month until the current month * generating a monthly aggregation files for the DStat system. * /* w w w . j a v a2 s . com*/ * @throws Exception */ private static void statInitial() throws Exception { //Output Prefix String outputPrefix = "dspace-log-monthly-"; // set up our command line variables String myLogDir = null; String myFileTemplate = null; String myConfigFile = null; StringBuffer myOutFile = null; Date myStartDate = null; Date myEndDate = null; boolean myLookUp = false; Calendar reportEndDate = new GregorianCalendar(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); Calendar currentMonth = (Calendar) reportStartDate.clone(); while (currentMonth.before(reportEndDate)) { Calendar start = new GregorianCalendar(currentMonth.get(Calendar.YEAR), currentMonth.get(Calendar.MONTH), currentMonth.getActualMinimum(Calendar.DAY_OF_MONTH)); myStartDate = start.getTime(); Calendar end = new GregorianCalendar(currentMonth.get(Calendar.YEAR), currentMonth.get(Calendar.MONTH), currentMonth.getActualMaximum(Calendar.DAY_OF_MONTH)); myEndDate = end.getTime(); myOutFile = new StringBuffer(outputLogDirectory); myOutFile.append(outputPrefix); myOutFile.append(currentMonth.get(Calendar.YEAR)); myOutFile.append("-"); myOutFile.append(currentMonth.get(Calendar.MONTH) + 1); myOutFile.append(outputSuffix); LogAnalyser.processLogs(context, myLogDir, myFileTemplate, myConfigFile, myOutFile.toString(), myStartDate, myEndDate, myLookUp); currentMonth.add(Calendar.MONTH, 1); } }
From source file:Main.java
static int daysBetween(Calendar day1, Calendar day2) { /**/*from w w w .j a v a 2 s . c o m*/ * Saved some effort using the solution described here, * http://stackoverflow.com/a/28865648/1587370 */ Calendar dayOne = (Calendar) day1.clone(), dayTwo = (Calendar) day2.clone(); if (dayOne.get(Calendar.YEAR) == dayTwo.get(Calendar.YEAR)) { return Math.abs(dayOne.get(Calendar.DAY_OF_YEAR) - dayTwo.get(Calendar.DAY_OF_YEAR)); } else { if (dayTwo.get(Calendar.YEAR) > dayOne.get(Calendar.YEAR)) { //swap them Calendar temp = dayOne; dayOne = dayTwo; dayTwo = temp; } int extraDays = 0; int dayOneOriginalYearDays = dayOne.get(Calendar.DAY_OF_YEAR); while (dayOne.get(Calendar.YEAR) > dayTwo.get(Calendar.YEAR)) { dayOne.add(Calendar.YEAR, -1); // getActualMaximum() important for leap years extraDays += dayOne.getActualMaximum(Calendar.DAY_OF_YEAR); } return extraDays - dayTwo.get(Calendar.DAY_OF_YEAR) + dayOneOriginalYearDays; } }
From source file:org.kuali.student.enrollment.class2.courseoffering.controller.CourseOfferingControllerPopulateUIForm.java
protected static void continueFromCreateCopyCourseOfferingInfo(CourseOfferingCreateWrapper coWrapper, CourseInfo course, TermInfo term) { ContextInfo contextInfo = ContextUtils.createDefaultContextInfo(); int firstValue = 0; try {//from w ww . j ava 2s . c o m List<CourseOfferingInfo> courseOfferingInfos = CourseOfferingManagementUtil.getCourseOfferingService() .getCourseOfferingsByCourseAndTerm(course.getId(), term.getId(), contextInfo); coWrapper.setCourse(course); coWrapper .setCreditCount( CourseOfferingViewHelperUtil .trimTrailing0(CourseOfferingManagementUtil.getLrcService() .getResultValue(course.getCreditOptions().get(firstValue) .getResultValueKeys().get(firstValue), contextInfo) .getValue())); coWrapper.setShowAllSections(true); coWrapper.setShowCopyCourseOffering(false); coWrapper.setShowTermOfferingLink(true); coWrapper.setContextBar(CourseOfferingContextBar.NEW_INSTANCE(coWrapper.getTerm(), coWrapper.getSocInfo(), CourseOfferingManagementUtil.getStateService(), CourseOfferingManagementUtil.getAcademicCalendarService(), contextInfo)); coWrapper.getExistingTermOfferings().clear(); coWrapper.getExistingOfferingsInCurrentTerm().clear(); for (CourseOfferingInfo courseOfferingInfo : courseOfferingInfos) { if (StringUtils.equals(courseOfferingInfo.getStateKey(), LuiServiceConstants.LUI_CO_STATE_OFFERED_KEY)) { CourseOfferingEditWrapper co = new CourseOfferingEditWrapper(courseOfferingInfo); co.setGradingOption( CourseOfferingManagementUtil.getGradingOption(courseOfferingInfo.getGradingOptionId())); coWrapper.getExistingOfferingsInCurrentTerm().add(co); } } //Get past 5 years CO Calendar termStart = Calendar.getInstance(); termStart.setTime(term.getStartDate()); String termYear = Integer.toString(termStart.get(Calendar.YEAR)); String termMonth = Integer.toString((termStart.get(Calendar.MONTH) + 1)); String termDayOfMonth = Integer.toString((termStart.getActualMaximum(Calendar.DAY_OF_MONTH))); org.kuali.student.r2.core.search.dto.SearchRequestInfo searchRequest = new org.kuali.student.r2.core.search.dto.SearchRequestInfo( CourseOfferingHistorySearchImpl.PAST_CO_SEARCH.getKey()); searchRequest.addParam(CourseOfferingHistorySearchImpl.COURSE_ID, coWrapper.getCourse().getId()); searchRequest.addParam(CourseOfferingHistorySearchImpl.TARGET_DAY_OF_MONTH_PARAM, termDayOfMonth); searchRequest.addParam(CourseOfferingHistorySearchImpl.TARGET_MONTH_PARAM, termMonth); searchRequest.addParam(CourseOfferingHistorySearchImpl.TARGET_YEAR_PARAM, termYear); searchRequest.addParam(CourseOfferingHistorySearchImpl.SearchParameters.CROSS_LIST_SEARCH_ENABLED, BooleanUtils.toStringTrueFalse(true)); org.kuali.student.r2.core.search.dto.SearchResultInfo searchResult = CourseOfferingManagementUtil .getSearchService().search(searchRequest, null); List<String> courseOfferingIds = new ArrayList<String>(searchResult.getTotalResults()); /* Checks whether the course is cross-listed and Set the codes that are cross listed to the cross-listed list */ for (org.kuali.student.r2.core.search.dto.SearchResultRowInfo row : searchResult.getRows()) { for (SearchResultCellInfo cellInfo : row.getCells()) { String value = StringUtils.EMPTY; if (cellInfo.getValue() != null) { value = cellInfo.getValue(); } if (CourseOfferingHistorySearchImpl.SearchResultColumns.CO_ID.equals(cellInfo.getKey())) { courseOfferingIds.add(value); } else if (CourseOfferingHistorySearchImpl.SearchResultColumns.IS_CROSS_LISTED .equals(cellInfo.getValue())) { coWrapper.setCrossListedCo(BooleanUtils.toBoolean(value)); } else if (CourseOfferingHistorySearchImpl.SearchResultColumns.CROSS_LISTED_COURSES .equals(cellInfo.getKey())) { coWrapper.setAlternateCOCodes(Arrays.asList(StringUtils.split(value, ","))); } } } /* * Avoid duplicates in case there is a cross Listed */ HashSet hs = new HashSet(); hs.addAll(courseOfferingIds); courseOfferingIds.clear(); courseOfferingIds.addAll(hs); courseOfferingInfos = CourseOfferingManagementUtil.getCourseOfferingService() .getCourseOfferingsByIds(courseOfferingIds, contextInfo); for (CourseOfferingInfo courseOfferingInfo : courseOfferingInfos) { CourseOfferingEditWrapper co = new CourseOfferingEditWrapper(courseOfferingInfo); TermInfo termInfo = CourseOfferingManagementUtil.getAcademicCalendarService() .getTerm(courseOfferingInfo.getTermId(), contextInfo); co.setTerm(termInfo); co.setGradingOption( CourseOfferingManagementUtil.getGradingOption(courseOfferingInfo.getGradingOptionId())); co.setAlternateCOCodes(coWrapper.getAlternateCOCodes()); coWrapper.getExistingTermOfferings().add(co); } } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.cognizant.trumobi.em.Email.java
public static int getTotalNumberOfDay(int month, int date, int year) { Calendar calendar = Calendar.getInstance(); calendar.set(year, month, 1);/*from www . j a v a2 s . c o m*/ int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); return days; }
From source file:org.mifos.framework.util.helpers.DateUtils.java
public static Date getLastDayOfCurrentYear() { Calendar cal = getCurrentDateCalendar(); cal.set(Calendar.MONTH, Calendar.DECEMBER); cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DATE)); Calendar cal1 = getCurrentDateCalendar(); cal1.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE), 0, 0, 0); return new Date(cal1.getTimeInMillis()); }