List of usage examples for org.joda.time MutableDateTime addMonths
public void addMonths(final int months)
From source file:com.tmathmeyer.sentinel.utils.Months.java
License:Open Source License
public static DateTime prevMonth(DateTime time) { MutableDateTime mdt = new MutableDateTime(time); mdt.addMonths(-1); return mdt.toDateTime(); }
From source file:controllers.api.DashboardsApiController.java
License:Open Source License
private void nextStep(String interval, MutableDateTime currentTime) { switch (interval) { case "minute": currentTime.addMinutes(1);/*from ww w.j a va 2 s .c o m*/ break; case "hour": currentTime.addHours(1); break; case "day": currentTime.addDays(1); break; case "week": currentTime.addWeeks(1); break; case "month": currentTime.addMonths(1); break; case "quarter": currentTime.addMonths(3); break; case "year": currentTime.addYears(1); break; default: throw new IllegalArgumentException("Invalid duration specified: " + interval); } }
From source file:ddf.metrics.reporting.internal.rrd4j.RrdMetricsRetriever.java
License:Open Source License
private void increment(MutableDateTime chunkStart, SUMMARY_INTERVALS summaryInterval) { switch (summaryInterval) { case minute:/*from w w w . j ava2 s . co m*/ chunkStart.addMinutes(1); break; case hour: chunkStart.addHours(1); break; case day: chunkStart.addDays(1); break; case week: chunkStart.addWeeks(1); break; case month: chunkStart.addMonths(1); break; } }
From source file:edu.wpi.cs.wpisuitetng.modules.cal.ui.navigation.MiniMonth.java
License:Open Source License
public MiniMonth(DateTime time, final MiniCalendarHostIface mc, boolean monthOnly) { this.setLayout(new GridLayout(7, 7)); MutableDateTime prevMonth = new MutableDateTime(time); prevMonth.setDayOfMonth(1);/*from w ww . j av a2 s.com*/ prevMonth.addMonths(-1); // What is prevMonth for? String[] dayLabel = { "S", "M", "T", "W", "R", "F", "S" }; MouseListener monthChanger = new MouseListener() { @Override public void mouseClicked(MouseEvent me) { } @Override public void mouseEntered(MouseEvent me) { } @Override public void mouseExited(MouseEvent me) { } @Override public void mousePressed(MouseEvent me) { } @Override public void mouseReleased(MouseEvent me) { DayLabel d = (DayLabel) (me.getSource()); if (!(d instanceof DescriptiveDayLabel)) { mc.display(d.getMonth()); } } }; MutableDateTime referenceDay = new MutableDateTime(time); // reset to the first of the month at midnight, then find Sunday referenceDay.setDayOfMonth(1); referenceDay.setMillisOfDay(0); int first = referenceDay.getDayOfWeek(); referenceDay.addDays(-first); boolean flipFlop = false; // add day labels for (int i = 0; i < 7; i++) { DayLabel day = new DescriptiveDayLabel(dayLabel[i], time); day.borderize((i % 7) == 0, i >= 5 * 7, (i % 7) == 6); add(day); day.addMouseListener(monthChanger); } // generate days, 6*7 covers all possible months, so we just loop // through and add each day for (int i = 0; i < (6 * 7); i++) { DayLabel day; if (monthOnly || MainPanel.getInstance().getView() == ViewSize.Month) { if (referenceDay.getDayOfMonth() == 1) flipFlop ^= true; // flops the flip flop flappity flip } else if (MainPanel.getInstance().getView() == ViewSize.Day) flipFlop = referenceDay.getDayOfYear() == time.getDayOfYear() && referenceDay.getYear() == time.getYear(); else if (MainPanel.getInstance().getView() == ViewSize.Week) { if (Months.getWeekStart(time).getMonthOfYear() == 12 && Months.getWeekStart(time).getDayOfMonth() >= 26) // Exception case for weeks between years flipFlop = time.getMonthOfYear() == 12 ? i >= 35 : i <= 6; else flipFlop = referenceDay.getDayOfYear() >= Months.getWeekStart(time).getDayOfYear() && referenceDay.getDayOfYear() <= Months.getWeekStart(time).getDayOfYear() + 6; } if (flipFlop) day = new ActiveDayLabel(referenceDay.toDateTime()); else day = new InactiveDayLabel(referenceDay.toDateTime()); day.borderize((i % 7) == 0, i >= 5 * 7, (i % 7) == 6); add(day); day.addMouseListener(monthChanger); referenceDay.addDays(1); // go to next day } }
From source file:fc.cron.CronExpression.java
License:Apache License
public DateTime nextTimeAfter(DateTime afterTime, DateTime dateTimeBarrier) { MutableDateTime nextTime = new MutableDateTime(afterTime); nextTime.setMillisOfSecond(0);/*from www. ja v a 2 s . c o m*/ nextTime.secondOfDay().add(1); while (true) { // day of week while (true) { // month while (true) { // day of month while (true) { // hour while (true) { // minute while (true) { // second if (secondField.matches(nextTime.getSecondOfMinute())) { break; } nextTime.secondOfDay().add(1); } if (minuteField.matches(nextTime.getMinuteOfHour())) { break; } nextTime.minuteOfDay().add(1); nextTime.secondOfMinute().set(0); } if (hourField.matches(nextTime.getHourOfDay())) { break; } nextTime.hourOfDay().add(1); nextTime.minuteOfHour().set(0); nextTime.secondOfMinute().set(0); } if (dayOfMonthField.matches(new LocalDate(nextTime))) { break; } nextTime.addDays(1); nextTime.setTime(0, 0, 0, 0); checkIfDateTimeBarrierIsReached(nextTime, dateTimeBarrier); } if (monthField.matches(nextTime.getMonthOfYear())) { break; } nextTime.addMonths(1); nextTime.setDayOfMonth(1); nextTime.setTime(0, 0, 0, 0); checkIfDateTimeBarrierIsReached(nextTime, dateTimeBarrier); } if (dayOfWeekField.matches(new LocalDate(nextTime))) { break; } nextTime.addDays(1); nextTime.setTime(0, 0, 0, 0); checkIfDateTimeBarrierIsReached(nextTime, dateTimeBarrier); } return nextTime.toDateTime(); }
From source file:ke.co.tawi.babblesms.server.session.SessionStatisticsFactory.java
License:Open Source License
/** * * @param accountUuid/*ww w .j a v a 2s. co m*/ * @return session statistics */ public static SessionStatistics getSessionStatistics(String accountUuid) { int count; //int[] arraycount = new int[2]; SessionStatistics stats = new SessionStatistics(); // Get the list of all networks List<Network> networkList = networkDAO.getAllNetworks(); ; //set the count of all incoming SMS stats.setAllIncomingSMSCount(countUtils.getIncomingCount(accountUuid)); //set the count of all outgoing SMS stats.setAllOutgoingSMSCount(countUtils.getOutgoingLog(accountUuid)); //set the count of all outgoing Group SMS //stats.setAllOutgoingSMSCount(countUtils.getOutgoingGroupLog(accountUuid)); // Set up data for the pie charts for (Network network : networkList) { //get the count of incoming SMS according to the account and network count = countUtils.getIncomingCount(accountUuid, network); //if count is greater than zero, add the information if (count > 0) { stats.addNetworkIncomingCount(network, count); } //get the count of outgoing SMS according to the account and network count = countUtils.getOutgoingCount(accountUuid, network); //if count is greater than zero, add the information if (count > 0) { stats.addNetworkOutgoingSMSCount(network, count); } } // Set up data for the bar charts DateTime dateMidnightStart = DateTime.now().minus(Hours.hours(24 * (IncomingBarDay.DAY_COUNT))); DateTime dateMidnightEnd = dateMidnightStart.plus(Hours.hours(24)); int numDays = 0; do { for (Network network : networkList) { //get the daily count for incoming count = countUtils.getIncomingCount(accountUuid, network, new Date(dateMidnightStart.getMillis()), new Date(dateMidnightEnd.getMillis())); if (count > 0) { stats.addNetworkIncomingUSSDCountDay( new SimpleDateFormat("MMM d").format(new Date(dateMidnightStart.getMillis())), network, count); } //get the daily count for outgoing count = countUtils.getOutgoingCount(accountUuid, network, new Date(dateMidnightStart.getMillis()), new Date(dateMidnightEnd.getMillis())); if (count > 0) { stats.addNetworkOutgoingUSSDCountDay( new SimpleDateFormat("MMM d").format(new Date(dateMidnightStart.getMillis())), network, count); } } dateMidnightStart = dateMidnightStart.plus(Hours.hours(24)); dateMidnightEnd = dateMidnightEnd.plus(Hours.hours(24)); numDays++; } while (numDays < IncomingBarDay.DAY_COUNT); // Set up data for the Weekly bar charts int numWeeks = 0; MutableDateTime startwkMutableDateTime = new MutableDateTime(); MutableDateTime endwkMutableDateTime; startwkMutableDateTime.setDayOfWeek(1); //get the first day of the week startwkMutableDateTime.setMillisOfDay(0); //get 00:00:00 time of the day //go back to 7 weeks startwkMutableDateTime.addWeeks(-7); do { // set the end date by creating a copy endwkMutableDateTime = new MutableDateTime(startwkMutableDateTime); //push it by one week endwkMutableDateTime.addWeeks(1); for (Network network : networkList) { //get the Weekly count for Incoming USSD count = countUtils.getIncomingCount(accountUuid, network, new Date(startwkMutableDateTime.toDate().getTime()), new Date(endwkMutableDateTime.toDate().getTime())); if (count > 0) { stats.addNetworkIncomingUSSDCountWeek( new SimpleDateFormat("MMM d").format(new Date(startwkMutableDateTime.getMillis())), network, count); } //get the Weekly count for Outgoing USSD count = countUtils.getOutgoingCount(accountUuid, network, new Date(startwkMutableDateTime.toDate().getTime()), new Date(endwkMutableDateTime.toDate().getTime())); if (count > 0) { stats.addNetworkOutgoingUSSDCountWeek( new SimpleDateFormat("MMM d").format(new Date(startwkMutableDateTime.getMillis())), network, count); } } // get the next week startwkMutableDateTime.addWeeks(1); numWeeks++; } while (numWeeks < 7); // Set up data for the monthly bar charts int numMonths = 0; MutableDateTime startMutableDateTime = new MutableDateTime(); MutableDateTime endMutableDateTime; startMutableDateTime.setDayOfMonth(1); //get the first day of the month startMutableDateTime.setMillisOfDay(0); //get 00:00:00 time of the day //go back to 6 months startMutableDateTime.addMonths(-5); do { //set the end date by creating a copy endMutableDateTime = new MutableDateTime(startMutableDateTime); //push it by one month endMutableDateTime.addMonths(1); //System.out.println("Start date: " + startMutableDateTime); //System.out.println("End date: " + endMutableDateTime); for (Network network : networkList) { //change to use millis count = countUtils.getIncomingCount(accountUuid, network, new Date(startMutableDateTime.toDate().getTime()), new Date(endMutableDateTime.toDate().getTime())); if (count > 0) { stats.addNetworkIncomingUSSDCountMonth( new SimpleDateFormat("MMM").format(new Date(startMutableDateTime.getMillis())), network, count); } //System.out.println(count); } //get the next month startMutableDateTime.addMonths(1); numMonths++; } while (numMonths < 6); return stats; }
From source file:org.codelibs.elasticsearch.common.joda.DateMathParser.java
License:Apache License
private long parseMath(String mathString, long time, boolean roundUp, DateTimeZone timeZone) throws ElasticsearchParseException { if (timeZone == null) { timeZone = DateTimeZone.UTC;// w w w . j a v a 2s . c om } MutableDateTime dateTime = new MutableDateTime(time, timeZone); for (int i = 0; i < mathString.length();) { char c = mathString.charAt(i++); final boolean round; final int sign; if (c == '/') { round = true; sign = 1; } else { round = false; if (c == '+') { sign = 1; } else if (c == '-') { sign = -1; } else { throw new ElasticsearchParseException("operator not supported for date math [{}]", mathString); } } if (i >= mathString.length()) { throw new ElasticsearchParseException("truncated date math [{}]", mathString); } final int num; if (!Character.isDigit(mathString.charAt(i))) { num = 1; } else { int numFrom = i; while (i < mathString.length() && Character.isDigit(mathString.charAt(i))) { i++; } if (i >= mathString.length()) { throw new ElasticsearchParseException("truncated date math [{}]", mathString); } num = Integer.parseInt(mathString.substring(numFrom, i)); } if (round) { if (num != 1) { throw new ElasticsearchParseException("rounding `/` can only be used on single unit types [{}]", mathString); } } char unit = mathString.charAt(i++); MutableDateTime.Property propertyToRound = null; switch (unit) { case 'y': if (round) { propertyToRound = dateTime.yearOfCentury(); } else { dateTime.addYears(sign * num); } break; case 'M': if (round) { propertyToRound = dateTime.monthOfYear(); } else { dateTime.addMonths(sign * num); } break; case 'w': if (round) { propertyToRound = dateTime.weekOfWeekyear(); } else { dateTime.addWeeks(sign * num); } break; case 'd': if (round) { propertyToRound = dateTime.dayOfMonth(); } else { dateTime.addDays(sign * num); } break; case 'h': case 'H': if (round) { propertyToRound = dateTime.hourOfDay(); } else { dateTime.addHours(sign * num); } break; case 'm': if (round) { propertyToRound = dateTime.minuteOfHour(); } else { dateTime.addMinutes(sign * num); } break; case 's': if (round) { propertyToRound = dateTime.secondOfMinute(); } else { dateTime.addSeconds(sign * num); } break; default: throw new ElasticsearchParseException("unit [{}] not supported for date math [{}]", unit, mathString); } if (propertyToRound != null) { if (roundUp) { // we want to go up to the next whole value, even if we are already on a rounded value propertyToRound.add(1); propertyToRound.roundFloor(); dateTime.addMillis(-1); // subtract 1 millisecond to get the largest inclusive value } else { propertyToRound.roundFloor(); } } } return dateTime.getMillis(); }
From source file:org.elasticsearch.common.joda.DateMathParser.java
License:Apache License
private long parseMath(String mathString, long time, boolean roundUp) throws ElasticsearchParseException { MutableDateTime dateTime = new MutableDateTime(time, DateTimeZone.UTC); try {//from w ww . j a va 2 s .com for (int i = 0; i < mathString.length();) { char c = mathString.charAt(i++); int type; if (c == '/') { type = 0; } else if (c == '+') { type = 1; } else if (c == '-') { type = 2; } else { throw new ElasticsearchParseException( "operator not supported for date math [" + mathString + "]"); } int num; if (!Character.isDigit(mathString.charAt(i))) { num = 1; } else { int numFrom = i; while (Character.isDigit(mathString.charAt(i))) { i++; } num = Integer.parseInt(mathString.substring(numFrom, i)); } if (type == 0) { // rounding is only allowed on whole numbers if (num != 1) { throw new ElasticsearchParseException( "rounding `/` can only be used on single unit types [" + mathString + "]"); } } char unit = mathString.charAt(i++); switch (unit) { case 'y': if (type == 0) { if (roundUp) { dateTime.yearOfCentury().roundCeiling(); } else { dateTime.yearOfCentury().roundFloor(); } } else if (type == 1) { dateTime.addYears(num); } else if (type == 2) { dateTime.addYears(-num); } break; case 'M': if (type == 0) { if (roundUp) { dateTime.monthOfYear().roundCeiling(); } else { dateTime.monthOfYear().roundFloor(); } } else if (type == 1) { dateTime.addMonths(num); } else if (type == 2) { dateTime.addMonths(-num); } break; case 'w': if (type == 0) { if (roundUp) { dateTime.weekOfWeekyear().roundCeiling(); } else { dateTime.weekOfWeekyear().roundFloor(); } } else if (type == 1) { dateTime.addWeeks(num); } else if (type == 2) { dateTime.addWeeks(-num); } break; case 'd': if (type == 0) { if (roundUp) { dateTime.dayOfMonth().roundCeiling(); } else { dateTime.dayOfMonth().roundFloor(); } } else if (type == 1) { dateTime.addDays(num); } else if (type == 2) { dateTime.addDays(-num); } break; case 'h': case 'H': if (type == 0) { if (roundUp) { dateTime.hourOfDay().roundCeiling(); } else { dateTime.hourOfDay().roundFloor(); } } else if (type == 1) { dateTime.addHours(num); } else if (type == 2) { dateTime.addHours(-num); } break; case 'm': if (type == 0) { if (roundUp) { dateTime.minuteOfHour().roundCeiling(); } else { dateTime.minuteOfHour().roundFloor(); } } else if (type == 1) { dateTime.addMinutes(num); } else if (type == 2) { dateTime.addMinutes(-num); } break; case 's': if (type == 0) { if (roundUp) { dateTime.secondOfMinute().roundCeiling(); } else { dateTime.secondOfMinute().roundFloor(); } } else if (type == 1) { dateTime.addSeconds(num); } else if (type == 2) { dateTime.addSeconds(-num); } break; default: throw new ElasticsearchParseException( "unit [" + unit + "] not supported for date math [" + mathString + "]"); } } } catch (Exception e) { if (e instanceof ElasticsearchParseException) { throw (ElasticsearchParseException) e; } throw new ElasticsearchParseException("failed to parse date math [" + mathString + "]"); } return dateTime.getMillis(); }