Java Code Examples for org.joda.time.Period#getSeconds()

The following examples show how to use org.joda.time.Period#getSeconds() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DateTimeService.java    From hawkular-metrics with Apache License 2.0 6 votes vote down vote up
public static DateTime getTimeSlice(DateTime dt, Duration duration) {
    Period p = duration.toPeriod();

    if (p.getYears() != 0) {
        return dt.yearOfEra().roundFloorCopy().minusYears(dt.getYearOfEra() % p.getYears());
    } else if (p.getMonths() != 0) {
        return dt.monthOfYear().roundFloorCopy().minusMonths((dt.getMonthOfYear() - 1) % p.getMonths());
    } else if (p.getWeeks() != 0) {
        return dt.weekOfWeekyear().roundFloorCopy().minusWeeks((dt.getWeekOfWeekyear() - 1) % p.getWeeks());
    } else if (p.getDays() != 0) {
        return dt.dayOfMonth().roundFloorCopy().minusDays((dt.getDayOfMonth() - 1) % p.getDays());
    } else if (p.getHours() != 0) {
        return dt.hourOfDay().roundFloorCopy().minusHours(dt.getHourOfDay() % p.getHours());
    } else if (p.getMinutes() != 0) {
        return dt.minuteOfHour().roundFloorCopy().minusMinutes(dt.getMinuteOfHour() % p.getMinutes());
    } else if (p.getSeconds() != 0) {
        return dt.secondOfMinute().roundFloorCopy().minusSeconds(dt.getSecondOfMinute() % p.getSeconds());
    }
    return dt.millisOfSecond().roundCeilingCopy().minusMillis(dt.getMillisOfSecond() % p.getMillis());
}
 
Example 2
Source File: ThirdEyeUtils.java    From incubator-pinot with Apache License 2.0 6 votes vote down vote up
/**
 * Guess time duration from period.
 *
 * @param granularity dataset granularity
 * @return
 */
public static int getTimeDuration(Period granularity) {
  if (granularity.getDays() > 0) {
    return granularity.getDays();
  }
  if (granularity.getHours() > 0) {
    return granularity.getHours();
  }
  if (granularity.getMinutes() > 0) {
    return granularity.getMinutes();
  }
  if (granularity.getSeconds() > 0) {
    return granularity.getSeconds();
  }
  return granularity.getMillis();
}
 
Example 3
Source File: ThirdEyeUtils.java    From incubator-pinot with Apache License 2.0 6 votes vote down vote up
/**
 * Guess time unit from period.
 *
 * @param granularity dataset granularity
 * @return
 */
public static TimeUnit getTimeUnit(Period granularity) {
  if (granularity.getDays() > 0) {
    return TimeUnit.DAYS;
  }
  if (granularity.getHours() > 0) {
    return TimeUnit.HOURS;
  }
  if (granularity.getMinutes() > 0) {
    return TimeUnit.MINUTES;
  }
  if (granularity.getSeconds() > 0) {
    return TimeUnit.SECONDS;
  }
  return TimeUnit.MILLISECONDS;
}
 
Example 4
Source File: DateUtils.java    From boot-actuator with MIT License 5 votes vote down vote up
/**
 * 取两个日期间隔
 * @param startDate
 * @param endDate
 * @return
 */
public static String getDateTimeBetween(Date startDate, Date endDate) {
    DateTime start = new DateTime(startDate.getTime());
    DateTime end = new DateTime(endDate.getTime());
    Period period = new Period(start, end);
    String result = period.getDays() + "天" + period.getHours() + "小时" + period.getMinutes() + "分" + period.getSeconds() + "秒";
    return result;
}
 
Example 5
Source File: TimeCalculation.java    From mangosta-android with Apache License 2.0 5 votes vote down vote up
private static String getTimeStringAgoSinceDateTime(Context context, DateTime dateTime) {
    DateTime now = new DateTime(DateTimeZone.getDefault());
    Period period = new Period(dateTime, now);
    String date;
    int count;

    if (period.getYears() >= 1) {
        date = context.getResources().getQuantityString(R.plurals.date_years_ago, period.getYears());
        count = period.getYears();
    } else if (period.getMonths() >= 1) {
        date = context.getResources().getQuantityString(R.plurals.date_months_ago, period.getMonths());
        count = period.getMonths();
    } else if (period.getWeeks() >= 1) {
        date = context.getResources().getQuantityString(R.plurals.date_weeks_ago, period.getWeeks());
        count = period.getWeeks();
    } else if (period.getDays() >= 1) {
        date = context.getResources().getQuantityString(R.plurals.date_days_ago, period.getDays());
        count = period.getDays();
    } else if (period.getHours() >= 1) {
        date = context.getResources().getQuantityString(R.plurals.date_hours_ago, period.getHours());
        count = period.getHours();
    } else if (period.getMinutes() >= 1) {
        date = context.getResources().getQuantityString(R.plurals.date_minutes_ago, period.getMinutes());
        count = period.getMinutes();
    } else if (period.getSeconds() >= 3) {
        date = String.format(Locale.getDefault(), context.getString(R.string.date_seconds_ago), period.getSeconds());
        count = period.getSeconds();
    } else {
        return " " + context.getString(R.string.date_seconds_now);
    }

    return String.format(Locale.getDefault(), date, count);
}
 
Example 6
Source File: Grouping.java    From incubator-pinot with Apache License 2.0 5 votes vote down vote up
private static DateTime makeOrigin(DateTime first, Period period) {
  if (period.getYears() > 0) {
    return first.year().roundFloorCopy().toDateTime();

  } else if (period.getMonths() > 0) {
    return first.monthOfYear().roundFloorCopy().toDateTime();

  } else if (period.getWeeks() > 0) {
    return first.weekOfWeekyear().roundFloorCopy().toDateTime();

  } else if (period.getDays() > 0) {
    return first.dayOfYear().roundFloorCopy().toDateTime();

  } else if (period.getHours() > 0) {
    return first.hourOfDay().roundFloorCopy().toDateTime();

  } else if (period.getMinutes() > 0) {
    return first.minuteOfHour().roundFloorCopy().toDateTime();

  } else if (period.getSeconds() > 0) {
    return first.secondOfMinute().roundFloorCopy().toDateTime();

  } else if (period.getMillis() > 0) {
    return first.millisOfSecond().roundFloorCopy().toDateTime();

  }

  throw new IllegalArgumentException(String.format("Unsupported Period '%s'", period));
}
 
Example 7
Source File: DateUtilities.java    From Bats with Apache License 2.0 4 votes vote down vote up
public static int periodToMillis(final Period period){
  return (period.getHours() * hoursToMillis) +
         (period.getMinutes() * minutesToMillis) +
         (period.getSeconds() * secondsToMillis) +
         (period.getMillis());
}
 
Example 8
Source File: JodaDateUtility.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
public static int millisFromPeriod(final Period period) {
  return (period.getHours() * hoursToMillis) +
    (period.getMinutes() * minutesToMillis) +
    (period.getSeconds() * secondsToMillis) +
    (period.getMillis());
}
 
Example 9
Source File: DURATION.java    From warp10-platform with Apache License 2.0 4 votes vote down vote up
@Override
public Object apply(WarpScriptStack stack) throws WarpScriptException {
  
  Object o = stack.pop();
  
  if (!(o instanceof String)) {
    throw new WarpScriptException(getName() + " expects an ISO8601 duration (a string) on top of the stack. See http://en.wikipedia.org/wiki/ISO_8601#Durations");
  }

  // Separate seconds from digits below second precision
  String duration_string = o.toString();
  String[] tokens = UnsafeString.split(duration_string, '.');

  long offset = 0;
  if (tokens.length > 2) {
    throw new WarpScriptException(getName() + "received an invalid ISO8601 duration.");
  }

  if (2 == tokens.length) {
    duration_string = tokens[0].concat("S");
    String tmp = tokens[1].substring(0, tokens[1].length() - 1);
    Double d_offset = Double.valueOf("0." + tmp) * STU;
    offset = d_offset.longValue();
  }
  
  ReadWritablePeriod period = new MutablePeriod();
  
  ISOPeriodFormat.standard().getParser().parseInto(period, duration_string, 0, Locale.US);

  Period p = period.toPeriod();
  
  if (p.getMonths() != 0 || p.getYears() != 0) {
    throw new WarpScriptException(getName() + " doesn't support ambiguous durations containing years or months, please convert those to days.");
  }

  Duration duration = p.toDurationFrom(new Instant());

  // check if offset should be positive of negative
  if (p.getSeconds() < 0) {
    offset = -offset;
  }

  stack.push(duration.getMillis() * Constants.TIME_UNITS_PER_MS + offset);

  return stack;
}
 
Example 10
Source File: RunDuration.java    From gocd with Apache License 2.0 4 votes vote down vote up
public long getTotalSeconds() {
    Period period = duration.toPeriod();
    return period.getHours()*3600 + period.getMinutes()*60 + period.getSeconds();
}