List of usage examples for org.joda.time MutableDateTime hourOfDay
public Property hourOfDay()
From source file:br.com.centralit.evm.citsmartevm.util.CronExpression.java
License:Apache License
public DateTime nextTimeAfter(DateTime afterTime) { MutableDateTime nextTime = new MutableDateTime(afterTime); nextTime.setMillisOfSecond(0);/* w w w. j a va2 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); } if (monthField.matches(nextTime.getMonthOfYear())) { break; } nextTime.addMonths(1); nextTime.setDayOfMonth(1); nextTime.setTime(0, 0, 0, 0); } if (dayOfWeekField.matches(new LocalDate(nextTime))) { break; } nextTime.addDays(1); nextTime.setTime(0, 0, 0, 0); } return nextTime.toDateTime(); }
From source file:com.google.gerrit.server.config.ScheduleConfig.java
License:Apache License
private static long initialDelay(Config rc, String section, String subsection, String keyStartTime, DateTime now, long interval) { long delay = MISSING_CONFIG; String start = rc.getString(section, subsection, keyStartTime); try {/* www . j a v a 2 s. c o m*/ if (start != null) { DateTimeFormatter formatter; MutableDateTime startTime = now.toMutableDateTime(); try { formatter = ISODateTimeFormat.hourMinute(); LocalTime firstStartTime = formatter.parseLocalTime(start); startTime.hourOfDay().set(firstStartTime.getHourOfDay()); startTime.minuteOfHour().set(firstStartTime.getMinuteOfHour()); } catch (IllegalArgumentException e1) { formatter = DateTimeFormat.forPattern("E HH:mm").withLocale(Locale.US); LocalDateTime firstStartDateTime = formatter.parseLocalDateTime(start); startTime.dayOfWeek().set(firstStartDateTime.getDayOfWeek()); startTime.hourOfDay().set(firstStartDateTime.getHourOfDay()); startTime.minuteOfHour().set(firstStartDateTime.getMinuteOfHour()); } startTime.secondOfMinute().set(0); startTime.millisOfSecond().set(0); long s = startTime.getMillis(); long n = now.getMillis(); delay = (s - n) % interval; if (delay <= 0) { delay += interval; } } else { log.info(MessageFormat.format("{0} schedule parameter \"{0}.{1}\" is not configured", section, keyStartTime)); } } catch (IllegalArgumentException e2) { log.error(MessageFormat.format("Invalid {0} schedule parameter \"{0}.{1}\"", section, keyStartTime), e2); delay = INVALID_CONFIG; } return delay; }
From source file:com.huffingtonpost.chronos.util.CronExpression.java
License:Apache License
@CoverageIgnore public DateTime nextTimeAfter(DateTime afterTime, DateTime dateTimeBarrier) { MutableDateTime nextTime = new MutableDateTime(afterTime); nextTime.setMillisOfSecond(0);// w w w . 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:controllers.api.DashboardsApiController.java
License:Open Source License
private void floorToBeginningOfInterval(String interval, MutableDateTime currentTime) { switch (interval) { case "minute": currentTime.minuteOfDay().roundFloor(); break;//from ww w. j a va 2s. co m case "hour": currentTime.hourOfDay().roundFloor(); break; case "day": currentTime.dayOfMonth().roundFloor(); break; case "week": currentTime.weekOfWeekyear().roundFloor(); break; case "month": currentTime.monthOfYear().roundFloor(); break; case "quarter": // set the month to the beginning of the quarter int currentQuarter = ((currentTime.getMonthOfYear() - 1) / 3); int startOfQuarter = (currentQuarter * 3) + 1; currentTime.setMonthOfYear(startOfQuarter); currentTime.monthOfYear().roundFloor(); break; case "year": currentTime.yearOfCentury().roundFloor(); break; default: throw new IllegalArgumentException("Invalid duration specified: " + interval); } }
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 w w w .ja va 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: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;// ww w .jav a2 s . co m } 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 w w . ja v a2 s . c o m*/ 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(); }
From source file:org.wso2.analytics.esb.util.TimeRangeUtils.java
License:Open Source License
public static List<TimeRange> getDateTimeRanges(long from, long to) { List<TimeRange> ranges = new ArrayList<>(10); MutableDateTime fromDate = new MutableDateTime(from); fromDate.set(DateTimeFieldType.millisOfSecond(), 0); MutableDateTime toDate = new MutableDateTime(to); toDate.set(DateTimeFieldType.millisOfSecond(), 0); MutableDateTime tempFromTime = fromDate.copy(); MutableDateTime tempToTime = toDate.copy(); if (log.isDebugEnabled()) { log.debug("Time range: " + formatter.format(fromDate.toDate()) + " -> " + formatter.format(toDate.toDate())); }//from w ww . j ava2 s . c om if (toDate.getMillis() - fromDate.getMillis() < DateTimeConstants.MILLIS_PER_MINUTE) { ranges.add(new TimeRange(RangeUnit.SECOND.name(), new long[] { fromDate.getMillis(), toDate.getMillis() })); } else { if (tempFromTime.getSecondOfMinute() != 0 && (toDate.getMillis() - fromDate.getMillis() > DateTimeConstants.MILLIS_PER_MINUTE)) { tempFromTime = tempFromTime.minuteOfHour().roundCeiling(); ranges.add(new TimeRange(RangeUnit.SECOND.name(), new long[] { fromDate.getMillis(), tempFromTime.getMillis() })); } if (tempFromTime.getMinuteOfHour() != 0 && ((toDate.getMillis() - tempFromTime.getMillis()) >= DateTimeConstants.MILLIS_PER_MINUTE)) { fromDate = tempFromTime.copy(); if (((toDate.getMillis() - tempFromTime.getMillis()) / DateTimeConstants.MILLIS_PER_MINUTE) < 60) { tempFromTime = tempFromTime.minuteOfHour().add( (toDate.getMillis() - tempFromTime.getMillis()) / DateTimeConstants.MILLIS_PER_MINUTE); } else { tempFromTime = tempFromTime.hourOfDay().roundCeiling(); } ranges.add(new TimeRange(RangeUnit.MINUTE.name(), new long[] { fromDate.getMillis(), tempFromTime.getMillis() })); } if (tempFromTime.getHourOfDay() != 0 && ((toDate.getMillis() - tempFromTime.getMillis()) >= DateTimeConstants.MILLIS_PER_HOUR)) { fromDate = tempFromTime.copy(); if (((toDate.getMillis() - tempFromTime.getMillis()) / DateTimeConstants.MILLIS_PER_HOUR) < 24) { tempFromTime = tempFromTime.hourOfDay().add( (toDate.getMillis() - tempFromTime.getMillis()) / DateTimeConstants.MILLIS_PER_HOUR); } else { tempFromTime = tempFromTime.dayOfMonth().roundCeiling(); } ranges.add(new TimeRange(RangeUnit.HOUR.name(), new long[] { fromDate.getMillis(), tempFromTime.getMillis() })); } if (tempFromTime.getDayOfMonth() != 1 && ((toDate.getMillis() - tempFromTime.getMillis()) >= DateTimeConstants.MILLIS_PER_DAY)) { fromDate = tempFromTime.copy(); if ((((toDate.getMillis() - tempFromTime.getMillis()) / DateTimeConstants.MILLIS_PER_DAY)) < tempFromTime.dayOfMonth().getMaximumValue()) { tempFromTime = tempFromTime.dayOfMonth().add(((toDate.getMillis() - tempFromTime.getMillis()) / ((long) DateTimeConstants.MILLIS_PER_DAY))); } else { tempFromTime = tempFromTime.monthOfYear().roundCeiling(); } ranges.add(new TimeRange(RangeUnit.DAY.name(), new long[] { fromDate.getMillis(), tempFromTime.getMillis() })); } if (tempToTime.getSecondOfMinute() != 0 && (tempToTime.getMillis() - tempFromTime.getMillis()) >= DateTimeConstants.MILLIS_PER_SECOND) { toDate = tempToTime.copy(); long remainingSeconds = ((toDate.getMillis() - tempFromTime.getMillis()) % DateTimeConstants.MILLIS_PER_MINUTE) / DateTimeConstants.MILLIS_PER_SECOND; if (remainingSeconds < 60) { tempToTime = tempToTime.secondOfMinute().add(-1 * remainingSeconds); } else { tempToTime = tempToTime.secondOfMinute().roundFloor(); } ranges.add(new TimeRange(RangeUnit.SECOND.name(), new long[] { tempToTime.getMillis(), toDate.getMillis() })); } if (tempToTime.getMinuteOfHour() != 0 && ((tempToTime.getMillis() - tempFromTime.getMillis()) >= DateTimeConstants.MILLIS_PER_MINUTE)) { toDate = tempToTime.copy(); long remainingMinutes = ((toDate.getMillis() - tempFromTime.getMillis()) % DateTimeConstants.MILLIS_PER_HOUR) / DateTimeConstants.MILLIS_PER_MINUTE; if (remainingMinutes < 60) { tempToTime = tempToTime.minuteOfHour().add(-1 * remainingMinutes); } else { tempToTime = tempToTime.hourOfDay().roundFloor(); } ranges.add(new TimeRange(RangeUnit.MINUTE.name(), new long[] { tempToTime.getMillis(), toDate.getMillis() })); } if (tempToTime.getHourOfDay() != 0 && ((tempToTime.getMillis() - tempFromTime.getMillis()) >= DateTimeConstants.MILLIS_PER_HOUR)) { toDate = tempToTime.copy(); long remainingHours = ((toDate.getMillis() - tempFromTime.getMillis()) % DateTimeConstants.MILLIS_PER_DAY) / DateTimeConstants.MILLIS_PER_HOUR; if (remainingHours < 24) { tempToTime = tempToTime.hourOfDay().add(-1 * remainingHours); } else { tempToTime = tempToTime.dayOfMonth().roundFloor(); } ranges.add(new TimeRange(RangeUnit.HOUR.name(), new long[] { tempToTime.getMillis(), toDate.getMillis() })); } if (tempToTime.getDayOfMonth() != 1 && ((tempToTime.getMillis() - tempFromTime.getMillis()) >= DateTimeConstants.MILLIS_PER_DAY)) { toDate = tempToTime.copy(); tempToTime = tempToTime.monthOfYear().roundFloor(); ranges.add(new TimeRange(RangeUnit.DAY.name(), new long[] { tempToTime.getMillis(), toDate.getMillis() })); } if (tempToTime.isAfter(tempFromTime)) { ranges.add(new TimeRange(RangeUnit.MONTH.name(), new long[] { tempFromTime.getMillis(), tempToTime.getMillis() })); } } if (log.isDebugEnabled()) { for (TimeRange timeRange : ranges) { log.debug("Unit: " + timeRange.getUnit() + " Range: " + formatter.format(new Date(timeRange.getRange()[0])) + "(" + timeRange.getRange()[0] + ")->" + formatter.format(new Date(timeRange.getRange()[1])) + "(" + timeRange.getRange()[1] + ")"); } } return ranges; }
From source file:org.wso2.analytics.shared.util.time.TimeRangeUtils.java
License:Open Source License
public static List<TimeRange> getDateTimeRanges(long from, long to) { List<TimeRange> ranges = new ArrayList<>(10); MutableDateTime fromDate = new MutableDateTime(from); fromDate.set(DateTimeFieldType.millisOfSecond(), 0); MutableDateTime toDate = new MutableDateTime(to); toDate.set(DateTimeFieldType.millisOfSecond(), 0); MutableDateTime tempFromTime = fromDate.copy(); MutableDateTime tempToTime = toDate.copy(); if (log.isDebugEnabled()) { log.debug("Time range: " + formatter.format(fromDate.toDate()) + "->" + formatter.format(toDate.toDate())); }/*from w ww. ja va 2 s. c o m*/ if (toDate.getMillis() - fromDate.getMillis() < DateTimeConstants.MILLIS_PER_MINUTE) { ranges.add(new TimeRange(RangeUnit.SECOND.name(), new long[] { fromDate.getMillis(), toDate.getMillis() })); } else { if (tempFromTime.getSecondOfMinute() != 0 && (toDate.getMillis() - fromDate.getMillis() > DateTimeConstants.MILLIS_PER_MINUTE)) { tempFromTime = tempFromTime.minuteOfHour().roundCeiling(); ranges.add(new TimeRange(RangeUnit.SECOND.name(), new long[] { fromDate.getMillis(), tempFromTime.getMillis() })); } if (tempFromTime.getMinuteOfHour() != 0 && ((toDate.getMillis() - tempFromTime.getMillis()) >= DateTimeConstants.MILLIS_PER_MINUTE)) { fromDate = tempFromTime.copy(); if (((toDate.getMillis() - tempFromTime.getMillis()) / DateTimeConstants.MILLIS_PER_MINUTE) < 60) { tempFromTime = tempFromTime.minuteOfHour().add( (toDate.getMillis() - tempFromTime.getMillis()) / DateTimeConstants.MILLIS_PER_MINUTE); } else { tempFromTime = tempFromTime.hourOfDay().roundCeiling(); } ranges.add(new TimeRange(RangeUnit.MINUTE.name(), new long[] { fromDate.getMillis(), tempFromTime.getMillis() })); } if (tempFromTime.getHourOfDay() != 0 && ((toDate.getMillis() - tempFromTime.getMillis()) >= DateTimeConstants.MILLIS_PER_HOUR)) { fromDate = tempFromTime.copy(); if (((toDate.getMillis() - tempFromTime.getMillis()) / DateTimeConstants.MILLIS_PER_HOUR) < 24) { tempFromTime = tempFromTime.hourOfDay().add( (toDate.getMillis() - tempFromTime.getMillis()) / DateTimeConstants.MILLIS_PER_HOUR); } else { tempFromTime = tempFromTime.dayOfMonth().roundCeiling(); } ranges.add(new TimeRange(RangeUnit.HOUR.name(), new long[] { fromDate.getMillis(), tempFromTime.getMillis() })); } if (tempFromTime.getDayOfMonth() != 1 && ((toDate.getMillis() - tempFromTime.getMillis()) >= DateTimeConstants.MILLIS_PER_DAY)) { fromDate = tempFromTime.copy(); if ((((toDate.getMillis() - tempFromTime.getMillis()) / DateTimeConstants.MILLIS_PER_DAY)) < tempFromTime.dayOfMonth().getMaximumValue()) { tempFromTime = tempFromTime.dayOfMonth().add(((toDate.getMillis() - tempFromTime.getMillis()) / ((long) DateTimeConstants.MILLIS_PER_DAY))); } else { tempFromTime = tempFromTime.monthOfYear().roundCeiling(); } ranges.add(new TimeRange(RangeUnit.DAY.name(), new long[] { fromDate.getMillis(), tempFromTime.getMillis() })); } if (tempToTime.getSecondOfMinute() != 0 && (tempToTime.getMillis() - tempFromTime.getMillis()) >= DateTimeConstants.MILLIS_PER_SECOND) { toDate = tempToTime.copy(); long remainingSeconds = ((toDate.getMillis() - tempFromTime.getMillis()) % DateTimeConstants.MILLIS_PER_MINUTE) / DateTimeConstants.MILLIS_PER_SECOND; if (remainingSeconds < 60) { tempToTime = tempToTime.secondOfMinute().add(-1 * remainingSeconds); } else { tempToTime = tempToTime.secondOfMinute().roundFloor(); } ranges.add(new TimeRange(RangeUnit.SECOND.name(), new long[] { tempToTime.getMillis(), toDate.getMillis() })); } if (tempToTime.getMinuteOfHour() != 0 && ((tempToTime.getMillis() - tempFromTime.getMillis()) >= DateTimeConstants.MILLIS_PER_MINUTE)) { toDate = tempToTime.copy(); long remainingMinutes = ((toDate.getMillis() - tempFromTime.getMillis()) % DateTimeConstants.MILLIS_PER_HOUR) / DateTimeConstants.MILLIS_PER_MINUTE; if (remainingMinutes < 60) { tempToTime = tempToTime.minuteOfHour().add(-1 * remainingMinutes); } else { tempToTime = tempToTime.hourOfDay().roundFloor(); } ranges.add(new TimeRange(RangeUnit.MINUTE.name(), new long[] { tempToTime.getMillis(), toDate.getMillis() })); } if (tempToTime.getHourOfDay() != 0 && ((tempToTime.getMillis() - tempFromTime.getMillis()) >= DateTimeConstants.MILLIS_PER_HOUR)) { toDate = tempToTime.copy(); long remainingHours = ((toDate.getMillis() - tempFromTime.getMillis()) % DateTimeConstants.MILLIS_PER_DAY) / DateTimeConstants.MILLIS_PER_HOUR; if (remainingHours < 24) { tempToTime = tempToTime.hourOfDay().add(-1 * remainingHours); } else { tempToTime = tempToTime.dayOfMonth().roundFloor(); } ranges.add(new TimeRange(RangeUnit.HOUR.name(), new long[] { tempToTime.getMillis(), toDate.getMillis() })); } if (tempToTime.getDayOfMonth() != 1 && ((tempToTime.getMillis() - tempFromTime.getMillis()) >= DateTimeConstants.MILLIS_PER_DAY)) { toDate = tempToTime.copy(); tempToTime = tempToTime.monthOfYear().roundFloor(); ranges.add(new TimeRange(RangeUnit.DAY.name(), new long[] { tempToTime.getMillis(), toDate.getMillis() })); } if (tempToTime.isAfter(tempFromTime)) { ranges.add(new TimeRange(RangeUnit.MONTH.name(), new long[] { tempFromTime.getMillis(), tempToTime.getMillis() })); } } if (log.isDebugEnabled()) { for (TimeRange timeRange : ranges) { log.debug("Unit: " + timeRange.getUnit() + " Range: " + formatter.format(new Date(timeRange.getRange()[0])) + "(" + timeRange.getRange()[0] + ")->" + formatter.format(new Date(timeRange.getRange()[1])) + "(" + timeRange.getRange()[1] + ")"); } } return ranges; }
From source file:org.wso2.analytics.shared.util.time.TimeRangeUtils.java
License:Open Source License
public static long getFloorValueForTimeStamp(long timestamp, String timeUnit) { RangeUnit timeUnitEnum = RangeUnit.valueOf(timeUnit); MutableDateTime mutableDateTime = new MutableDateTime(timestamp); long newFloorTimestamp = mutableDateTime.getMillis(); switch (timeUnitEnum) { case MONTH:/*from w w w . j a v a 2 s.c o m*/ newFloorTimestamp = mutableDateTime.monthOfYear().roundFloor().getMillis(); break; case DAY: newFloorTimestamp = mutableDateTime.dayOfMonth().roundFloor().getMillis(); break; case HOUR: newFloorTimestamp = mutableDateTime.hourOfDay().roundFloor().getMillis(); break; } if (log.isDebugEnabled()) { log.debug("Original date: " + formatter.format(new Date(timestamp)) + " -> Rounded date:" + formatter.format(new Date(newFloorTimestamp))); } return newFloorTimestamp; }