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

The following examples show how to use org.joda.time.Period#getHours() . 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: MyEvent.java    From projectforge-webapp with GNU General Public License v3.0 6 votes vote down vote up
public String getDuration()
{
  if (duration != null) {
    return duration;
  }
  final Period period = new Period(this.getStart(), this.getEnd());
  int days = period.getDays();
  if (isAllDay() == true) {
    ++days;
  }
  final int hours = period.getHours();
  final int minutes = period.getMinutes();
  final StringBuffer buf = new StringBuffer();
  if (days > 0) { // days
    buf.append(days).append(PFUserContext.getLocalizedString("calendar.unit.day")).append(" ");
  }
  if (isAllDay() == false) {
    buf.append(hours).append(":"); // hours
    if (minutes < 10) {
      buf.append("0");
    }
    buf.append(minutes);
  }
  duration = buf.toString();
  return duration;
}
 
Example 5
Source File: AlarmContentUtils.java    From sleep-cycle-alarm with GNU General Public License v3.0 5 votes vote down vote up
public static String getSummary(DateTime currentDate, DateTime executionDate) {
    String itemSummary = getString(R.string.list_element_summary_changeable);

    Period periodOfTime = getPeriodOfTime(currentDate, executionDate);
    long diffHours = periodOfTime.getHours();
    long diffMinutes = periodOfTime.getMinutes();

    return String.format(itemSummary,
            getSleepDurationString(diffHours, diffMinutes), getSleepHealthStatus(diffHours));
}
 
Example 6
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 7
Source File: MediathekShow.java    From zapp with MIT License 5 votes vote down vote up
public String getFormattedDuration() {
	int duration;
	try {
		duration = Integer.parseInt(this.duration);
	} catch (NumberFormatException e) {
		return "?";
	}

	Period period = Duration.standardSeconds(duration).toPeriod();
	PeriodFormatter formatter = (period.getHours() > 0) ? hourPeriodFormatter : secondsPeriodFormatter;
	return period.toString(formatter);
}
 
Example 8
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 9
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 10
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 11
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 12
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();
}