List of usage examples for org.joda.time DateTime withDayOfMonth
public DateTime withDayOfMonth(int dayOfMonth)
From source file:org.mifos.dmt.business.Meetings.schedule.MonthlyScheduleWithROD.java
License:Open Source License
public void generateSchedule(int numberOfDates) { numberOfDates--;//from w w w.j a v a 2 s .com getMeetingStartDate(); fastForwardTillDisbursement(); DateTime meetingScheduleDate = this.startDate; Date meetingDateInMifosFormat; for (int i = 0; i <= numberOfDates; i++) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); meetingDateInMifosFormat = getWorkingDay(meetingScheduleDate).toDate(); this.scheduleList.add(formatter.format(meetingDateInMifosFormat)); meetingScheduleDate = meetingScheduleDate.plusMonths(this.recAfter); meetingScheduleDate = meetingScheduleDate.withDayOfMonth(1); meetingScheduleDate = meetingScheduleDate.plusWeeks((this.rankOfDays - 1)); meetingScheduleDate = meetingScheduleDate.withDayOfWeek(this.days); } }
From source file:org.mitre.xtemporal.DateNormalization.java
License:Apache License
/** * For now this reports only DATEs -- no TIME parsing is handled. DateFormat * would work,,... except the wild variation of matching formats would be * difficult to handle./*w ww . java2 s.c o m*/ * * @param elements * @return void * @throws java.text.ParseException */ public static void normalize_date(java.util.Map<String, String> elements, DateMatch dt) throws java.text.ParseException { // Parse years. int year = normalize_year(elements); if (year == INVALID_DATE) { return; } if (year > MAXIMUM_YEAR) { // HHMM can look like a year, e.g., 2100h or 2300 PM return; } dt.resolution = DateMatch.TimeResolution.YEAR; int month = normalize_month(elements); if (month == INVALID_DATE) { month = normalize_month_name(elements); } if (month == INVALID_DATE) { return; } DateTime _cal = new DateTime(year, month, 1, 0, 0, DateTimeZone.UTC); dt.resolution = DateMatch.TimeResolution.MONTH; int dom = normalize_day(elements); // If you got this far, then assume Day of Month is 1 (first of the month) if (dom == INVALID_DATE) { // No date found, resolution is month dom = 1; } else if (dom == 0) { return; } else { dt.resolution = DateMatch.TimeResolution.DAY; } // Normalize Time fields found, H, M, s.SSS, etc. // DateTime _cal2 = _cal.withDayOfMonth(dom); dt.datenorm = new Date(_cal2.getMillis()); }
From source file:org.obm.imap.archive.services.SchedulingDatesService.java
License:Open Source License
private DateTime nextTreatmentDateCommonDayOfMonth(SchedulingConfiguration schedulingConfiguration, DateTime currentDateTime, DateTime currentDateWithScheduledTime) { DateTime dayOfMonth = currentDateWithScheduledTime .withDayOfMonth(schedulingConfiguration.getDayOfMonth().getDayIndex()); if (dayOfMonth.isAfter(currentDateTime)) { return dayOfMonth; }// w w w .j a va 2 s.c om return dayOfMonth.plusMonths(1); }
From source file:org.opencastproject.index.service.catalog.adapter.DublinCoreMetadataUtil.java
License:Educational Community License
/** * Gets the current hour, minute and second from a dublin core period if available. * * @param period//from w w w . j av a 2 s . co m * The current period from dublin core. * @return A new DateTime with the current hour, minute and second. */ static DateTime getCurrentStartDate(Opt<DCMIPeriod> period) { DateTime currentStartDate = new DateTime(); currentStartDate = currentStartDate.withZone(DateTimeZone.UTC); currentStartDate = currentStartDate.withYear(2001); currentStartDate = currentStartDate.withMonthOfYear(1); currentStartDate = currentStartDate.withDayOfMonth(1); if (period.isSome() && period.get().hasStart()) { DateTime fromDC = new DateTime(period.get().getStart().getTime()); fromDC = fromDC.withZone(DateTimeZone.UTC); currentStartDate = currentStartDate.withZone(DateTimeZone.UTC); currentStartDate = currentStartDate.withYear(fromDC.getYear()); currentStartDate = currentStartDate.withMonthOfYear(fromDC.getMonthOfYear()); currentStartDate = currentStartDate.withDayOfMonth(fromDC.getDayOfMonth()); } return currentStartDate; }
From source file:org.opencastproject.index.service.catalog.adapter.DublinCoreMetadataUtil.java
License:Educational Community License
/** * Gets the current hour, minute and second from a dublin core period if available. * * @param period// w w w. j ava 2 s . c o m * The current period from dublin core. * @return A new DateTime with the current hour, minute and second. */ private static DateTime getCurrentStartDateTime(Opt<DCMIPeriod> period) { DateTime currentStartTime = new DateTime(); currentStartTime = currentStartTime.withZone(DateTimeZone.UTC); currentStartTime = currentStartTime.withYear(2001); currentStartTime = currentStartTime.withMonthOfYear(1); currentStartTime = currentStartTime.withDayOfMonth(1); currentStartTime = currentStartTime.withHourOfDay(0); currentStartTime = currentStartTime.withMinuteOfHour(0); currentStartTime = currentStartTime.withSecondOfMinute(0); if (period.isSome() && period.get().hasStart()) { DateTime fromDC = new DateTime(period.get().getStart().getTime()); fromDC = fromDC.withZone(DateTimeZone.UTC); currentStartTime = currentStartTime.withZone(DateTimeZone.UTC); currentStartTime = currentStartTime.withYear(fromDC.getYear()); currentStartTime = currentStartTime.withMonthOfYear(fromDC.getMonthOfYear()); currentStartTime = currentStartTime.withDayOfMonth(fromDC.getDayOfMonth()); currentStartTime = currentStartTime.withHourOfDay(fromDC.getHourOfDay()); currentStartTime = currentStartTime.withMinuteOfHour(fromDC.getMinuteOfHour()); currentStartTime = currentStartTime.withSecondOfMinute(fromDC.getSecondOfMinute()); } return currentStartTime; }
From source file:org.opencastproject.index.service.catalog.adapter.DublinCoreMetadataUtil.java
License:Educational Community License
/** * Sets the start time in a dublin core catalog to the right value and keeps the start date and duration the same. * * @param dc/*from www . j av a 2 s .co m*/ * The dublin core catalog to adjust * @param field * The metadata field that contains the start time. * @param ename * The EName in the catalog to identify the property that has the dublin core period. */ static void setTemporalStartTime(DublinCoreCatalog dc, MetadataField<?> field, EName ename) { if (field.getValue().isNone() || (field.getValue().get() instanceof String && StringUtils.isBlank(field.getValue().get().toString()))) { logger.debug("No value was set for metadata field with dublin core id '{}' and json id '{}'", field.getInputID(), field.getOutputID()); return; } try { // Get the current date SimpleDateFormat dateFormat = MetadataField.getSimpleDateFormatter(field.getPattern().get()); Date startDate = dateFormat.parse((String) field.getValue().get()); // Get the current period Opt<DCMIPeriod> period = getPeriodFromCatalog(dc, ename); // Get the current duration Long duration = getDuration(period); // Get the current start date DateTime currentStartDate = getCurrentStartDate(period); // Setup the new start time DateTime startDateTime = new DateTime(startDate.getTime()); startDateTime = startDateTime.withZone(DateTimeZone.UTC); startDateTime = startDateTime.withYear(currentStartDate.getYear()); startDateTime = startDateTime.withMonthOfYear(currentStartDate.getMonthOfYear()); startDateTime = startDateTime.withDayOfMonth(currentStartDate.getDayOfMonth()); // Get the current end date based on new date and duration. DateTime endDate = new DateTime(startDateTime.toDate().getTime() + duration); dc.set(ename, EncodingSchemeUtils.encodePeriod(new DCMIPeriod(startDateTime.toDate(), endDate.toDate()), Precision.Second)); } catch (ParseException e) { logger.error("Not able to parse date {} to update the dublin core because: {}", field.getValue(), ExceptionUtils.getStackTrace(e)); } }
From source file:org.openlmis.report.service.lookup.ReportLookupService.java
License:Open Source License
public DateTime periodStartDate(Long range) { DateTime periodEndDate = periodEndDate(); if (range == 1) return periodEndDate.withDayOfMonth(1); else if (range == 2) return periodEndDate.minusMonths(2).withDayOfMonth(1); else if (range == 3) return periodEndDate.minusMonths(5).withDayOfMonth(1); else if (range == 4) return periodEndDate.minusYears(1).withDayOfMonth(1); return null;/*from w w w . java 2s .co m*/ }
From source file:org.opensextant.extractors.xtemporal.DateNormalization.java
License:Apache License
/** * For now this reports only DATE and standard TIME fields. Timezone is * still TODO./*ww w. ja v a 2 s. co m*/ * * TODO: throw NormalizationException * * @param elements pattern fields * @param dt found date * @throws ParseException the parse exception */ public static void normalize_date(java.util.Map<String, String> elements, DateMatch dt) throws ParseException { // Parse years. int year = normalize_year(elements); if (year == INVALID_DATE) { return; } if (year > MAXIMUM_YEAR) { // HHMM can look like a year, e.g., 2100h or 2300 PM return; } dt.resolution = DateMatch.TimeResolution.YEAR; int month = normalize_month(elements); if (month == INVALID_DATE) { month = normalize_month_name(elements); } if (month == INVALID_DATE) { return; } DateTime _cal = new DateTime(year, month, 1, 0, 0, DateTimeZone.UTC); dt.resolution = DateMatch.TimeResolution.MONTH; int dom = normalize_day(elements); // If you got this far, then assume Day of Month is 1 (first of the month) if (dom == INVALID_DATE) { // No date found, resolution is month dom = 1; } else if (dom == 0) { return; } else { dt.resolution = DateMatch.TimeResolution.DAY; } // Normalize Time fields found, H, M, s.SSS, etc. // _cal = _cal.withDayOfMonth(dom); // For normal M/D/Y patterns, set the default time to noon, UTC // Overall, we want to ensure that the general yyyy-mm-dd form is not impacted // by time zone and default hour of 00:00; -- this generally would yield a date format a day early for ALL US timezones. // // Time res: the presence of a field, hh, mm, or ss means the pattern has that level of resolution. // So even if time is 00:00:00Z -- all zeroes -- the resolution is still SECONDS. // int hour = normalize_time(elements, "hh"); if (hour >= 0) { // Only if HH:MM... is present do we try to detect TZ. // DateTimeZone tz = normalize_tz(elements); if (tz != null) { _cal = _cal.withZone(tz); } // NON-zero hour. dt.resolution = DateMatch.TimeResolution.HOUR; int min = normalize_time(elements, "mm"); if (min >= 0) { dt.resolution = DateMatch.TimeResolution.MINUTE; // NON-zero minutes _cal = _cal.withHourOfDay(hour); _cal = _cal.withMinuteOfHour(min); } else { // No minutes _cal = _cal.withHourOfDay(hour); } } else { // No hour; default is 12:00 UTC. _cal = _cal.withHourOfDay(12); } dt.datenorm = new Date(_cal.getMillis()); }
From source file:org.projectforge.web.calendar.TimesheetEventsProvider.java
License:Open Source License
/** * @see org.projectforge.web.calendar.MyFullCalendarEventsProvider#buildEvents(org.joda.time.DateTime, * org.joda.time.DateTime)//from w ww . j ava2 s .c o m */ @Override protected void buildEvents(final DateTime start, final DateTime end) { totalDuration = 0; for (int i = 0; i < durationsPerDayOfMonth.length; i++) { durationsPerDayOfMonth[i] = 0; } for (int i = 0; i < durationsPerDayOfYear.length; i++) { durationsPerDayOfYear[i] = 0; } final Integer userId = calFilter.getTimesheetUserId(); if (userId == null) { return; } breaksMap = new HashMap<String, TimesheetDO>(); int breaksCounter = 0; final TimesheetFilter filter = new TimesheetFilter(); filter.setUserId(userId); filter.setStartTime(start.toDate()); filter.setStopTime(end.toDate()); filter.setOrderType(OrderDirection.ASC); timesheets = timesheetDao.getList(filter); boolean longFormat = false; days = Days.daysBetween(start, end).getDays(); if (days < 10) { // Week or day view: longFormat = true; month = null; firstDayOfMonth = null; } else { // Month view: final DateTime currentMonth = new DateTime(start.plusDays(10), ThreadLocalUserContext.getDateTimeZone()); // Now we're definitely in the right // month. month = currentMonth.getMonthOfYear(); firstDayOfMonth = currentMonth.withDayOfMonth(1); } if (CollectionUtils.isEmpty(timesheets) == false) { DateTime lastStopTime = null; for (final TimesheetDO timesheet : timesheets) { final DateTime startTime = new DateTime(timesheet.getStartTime(), ThreadLocalUserContext.getDateTimeZone()); final DateTime stopTime = new DateTime(timesheet.getStopTime(), ThreadLocalUserContext.getDateTimeZone()); if (stopTime.isBefore(start) == true || startTime.isAfter(end) == true) { // Time sheet doesn't match time period start - end. continue; } if (calFilter.isShowBreaks() == true) { if (lastStopTime != null && DateHelper.isSameDay(stopTime, lastStopTime) == true && startTime.getMillis() - lastStopTime.getMillis() > 60000) { // Show breaks between time sheets of one day (> 60s). final Event breakEvent = new Event(); breakEvent.setEditable(false); final String breakId = String.valueOf(++breaksCounter); breakEvent.setClassName(Const.BREAK_EVENT_CLASS_NAME).setId(breakId).setStart(lastStopTime) .setEnd(startTime).setTitle(getString("timesheet.break")); breakEvent.setTextColor("#666666").setBackgroundColor("#F9F9F9").setColor("#F9F9F9"); events.put(breakId, breakEvent); final TimesheetDO breakTimesheet = new TimesheetDO().setStartDate(lastStopTime.toDate()) .setStopTime(startTime.getMillis()); breaksMap.put(breakId, breakTimesheet); } lastStopTime = stopTime; } final long duration = timesheet.getDuration(); final MyWicketEvent event = new MyWicketEvent(); final String id = "" + timesheet.getId(); event.setClassName(Const.EVENT_CLASS_NAME); event.setId(id); event.setStart(startTime); event.setEnd(stopTime); final String title = CalendarHelper.getTitle(timesheet); if (longFormat == true) { // Week or day view: event.setTitle(title + "\n" + getToolTip(timesheet) + "\n" + formatDuration(duration, false)); } else { // Month view: event.setTitle(title); } if (month != null && startTime.getMonthOfYear() != month && stopTime.getMonthOfYear() != month) { // Display time sheets of other month as grey blue: event.setTextColor("#222222").setBackgroundColor("#ACD9E8").setColor("#ACD9E8"); } events.put(id, event); if (month == null || startTime.getMonthOfYear() == month) { totalDuration += duration; addDurationOfDay(startTime.getDayOfMonth(), duration); } final int dayOfYear = startTime.getDayOfYear(); addDurationOfDayOfYear(dayOfYear, duration); event.setTooltip(getString("timesheet"), new String[][] { { title }, { timesheet.getLocation(), getString("timesheet.location") }, { KostFormatter.formatLong(timesheet.getKost2()), getString("fibu.kost2") }, { TaskFormatter.getTaskPath(timesheet.getTaskId(), true, OutputType.PLAIN), getString("task") }, { timesheet.getDescription(), getString("description") } }); } } if (calFilter.isShowStatistics() == true) { // Show statistics: duration of every day is shown as all day event. DateTime day = start; final Calendar cal = DateHelper.getCalendar(); cal.setTime(start.toDate()); final int numberOfDaysInYear = cal.getActualMaximum(Calendar.DAY_OF_YEAR); int paranoiaCounter = 0; do { if (++paranoiaCounter > 1000) { log.error( "Paranoia counter exceeded! Dear developer, please have a look at the implementation of buildEvents."); break; } final int dayOfYear = day.getDayOfYear(); final long duration = durationsPerDayOfYear[dayOfYear]; final boolean firstDayOfWeek = day.getDayOfWeek() == ThreadLocalUserContext.getJodaFirstDayOfWeek(); if (firstDayOfWeek == false && duration == 0) { day = day.plusDays(1); continue; } final Event event = new Event().setAllDay(true); final String id = "s-" + (dayOfYear); event.setId(id); event.setStart(day); final String durationString = formatDuration(duration, false); if (firstDayOfWeek == true) { // Show week of year at top of first day of week. long weekDuration = 0; for (short i = 0; i < 7; i++) { int d = dayOfYear + i; if (d > numberOfDaysInYear) { d -= numberOfDaysInYear; } weekDuration += durationsPerDayOfYear[d]; } final StringBuffer buf = new StringBuffer(); buf.append(getString("calendar.weekOfYearShortLabel")).append(DateHelper.getWeekOfYear(day)); if (days > 1 && weekDuration > 0) { // Show total sum of durations over all time sheets of current week (only in week and month view). buf.append(": ").append(formatDuration(weekDuration, false)); } if (duration > 0) { buf.append(", ").append(durationString); } event.setTitle(buf.toString()); } else { event.setTitle(durationString); } event.setTextColor("#666666").setBackgroundColor("#F9F9F9").setColor("#F9F9F9"); event.setEditable(false); events.put(id, event); day = day.plusDays(1); } while (day.isAfter(end) == false); } }
From source file:org.projectforge.web.timesheet.TimesheetEventsProvider.java
License:Open Source License
/** * @see org.projectforge.web.calendar.MyFullCalendarEventsProvider#buildEvents(org.joda.time.DateTime, org.joda.time.DateTime) *//*from w w w .j av a2 s . c o m*/ @Override protected void buildEvents(final DateTime start, final DateTime end) { totalDuration = 0; for (int i = 0; i < durationsPerDayOfMonth.length; i++) { durationsPerDayOfMonth[i] = 0; } for (int i = 0; i < durationsPerDayOfYear.length; i++) { durationsPerDayOfYear[i] = 0; } final Integer userId = calFilter.getTimesheetUserId(); if (userId == null) { return; } breaksMap = new HashMap<String, TimesheetDO>(); int breaksCounter = 0; final TimesheetFilter filter = new TimesheetFilter(); filter.setUserId(userId); filter.setStartTime(start.toDate()); filter.setStopTime(end.toDate()); filter.setOrderType(OrderDirection.ASC); timesheets = timesheetDao.getList(filter); boolean longFormat = false; days = Days.daysBetween(start, end).getDays(); if (days < 10) { // Week or day view: longFormat = true; month = null; firstDayOfMonth = null; } else { // Month view: final DateTime currentMonth = new DateTime(start.plusDays(10), PFUserContext.getDateTimeZone()); // Now we're definitely in the right // month. month = currentMonth.getMonthOfYear(); firstDayOfMonth = currentMonth.withDayOfMonth(1); } if (CollectionUtils.isEmpty(timesheets) == false) { DateTime lastStopTime = null; for (final TimesheetDO timesheet : timesheets) { final DateTime startTime = new DateTime(timesheet.getStartTime(), PFUserContext.getDateTimeZone()); final DateTime stopTime = new DateTime(timesheet.getStopTime(), PFUserContext.getDateTimeZone()); if (stopTime.isBefore(start) == true || startTime.isAfter(end) == true) { // Time sheet doesn't match time period start - end. continue; } if (calFilter.isShowBreaks() == true) { if (lastStopTime != null && DateHelper.isSameDay(stopTime, lastStopTime) == true && startTime.getMillis() - lastStopTime.getMillis() > 60000) { // Show breaks between time sheets of one day (> 60s). final Event breakEvent = new Event(); breakEvent.setEditable(false); final String breakId = String.valueOf(++breaksCounter); breakEvent.setClassName(BREAK_EVENT_CLASS_NAME).setId(breakId).setStart(lastStopTime) .setEnd(startTime).setTitle(getString("timesheet.break")); breakEvent.setTextColor("#666666").setBackgroundColor("#F9F9F9").setColor("#F9F9F9"); events.put(breakId, breakEvent); final TimesheetDO breakTimesheet = new TimesheetDO().setStartDate(lastStopTime.toDate()) .setStopTime(startTime.getMillis()); breaksMap.put(breakId, breakTimesheet); } lastStopTime = stopTime; } final long duration = timesheet.getDuration(); final MyEvent event = new MyEvent(); final String id = "" + timesheet.getId(); event.setClassName(EVENT_CLASS_NAME); event.setId(id); event.setStart(startTime); event.setEnd(stopTime); final String title = getTitle(timesheet); if (longFormat == true) { // Week or day view: event.setTitle(title + "\n" + getToolTip(timesheet) + "\n" + formatDuration(duration, false)); } else { // Month view: event.setTitle(title); } if (month != null && startTime.getMonthOfYear() != month && stopTime.getMonthOfYear() != month) { // Display time sheets of other month as grey blue: event.setTextColor("#222222").setBackgroundColor("#ACD9E8").setColor("#ACD9E8"); } events.put(id, event); if (month == null || startTime.getMonthOfYear() == month) { totalDuration += duration; addDurationOfDay(startTime.getDayOfMonth(), duration); } final int dayOfYear = startTime.getDayOfYear(); addDurationOfDayOfYear(dayOfYear, duration); event.setTooltip(getString("timesheet"), new String[][] { { title }, { timesheet.getLocation(), getString("timesheet.location") }, { KostFormatter.formatLong(timesheet.getKost2()), getString("fibu.kost2") }, { TaskFormatter.instance().getTaskPath(timesheet.getTaskId(), true, OutputType.PLAIN), getString("task") }, { timesheet.getDescription(), getString("description") } }); } } if (calFilter.isShowStatistics() == true) { // Show statistics: duration of every day is shown as all day event. DateTime day = start; final Calendar cal = DateHelper.getCalendar(); cal.setTime(start.toDate()); final int numberOfDaysInYear = cal.getActualMaximum(Calendar.DAY_OF_YEAR); int paranoiaCounter = 0; do { if (++paranoiaCounter > 1000) { log.error( "Paranoia counter exceeded! Dear developer, please have a look at the implementation of buildEvents."); break; } final int dayOfYear = day.getDayOfYear(); final long duration = durationsPerDayOfYear[dayOfYear]; final boolean firstDayOfWeek = day.getDayOfWeek() == PFUserContext.getJodaFirstDayOfWeek(); if (firstDayOfWeek == false && duration == 0) { day = day.plusDays(1); continue; } final Event event = new Event().setAllDay(true); final String id = "s-" + (dayOfYear); event.setId(id); event.setStart(day); final String durationString = formatDuration(duration, false); if (firstDayOfWeek == true) { // Show week of year at top of first day of week. long weekDuration = 0; for (short i = 0; i < 7; i++) { int d = dayOfYear + i; if (d > numberOfDaysInYear) { d -= numberOfDaysInYear; } weekDuration += durationsPerDayOfYear[d]; } final StringBuffer buf = new StringBuffer(); buf.append(getString("calendar.weekOfYearShortLabel")).append(DateHelper.getWeekOfYear(day)); if (days > 1 && weekDuration > 0) { // Show total sum of durations over all time sheets of current week (only in week and month view). buf.append(": ").append(formatDuration(weekDuration, false)); } if (duration > 0) { buf.append(", ").append(durationString); } event.setTitle(buf.toString()); } else { event.setTitle(durationString); } event.setTextColor("#666666").setBackgroundColor("#F9F9F9").setColor("#F9F9F9"); event.setEditable(false); events.put(id, event); day = day.plusDays(1); } while (day.isAfter(end) == false); } }