Java Code Examples for org.joda.time.Duration#getStandardDays()

The following examples show how to use org.joda.time.Duration#getStandardDays() . 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: DateUtils.java    From xmu-2016-MrCode with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 获得两个时间点之间的时间跨度
 * 
 * @param time1
 *            开始的时间点
 * @param time2
 *            结束的时间点
 * @param timeUnit
 *            跨度的时间单位 see {@link JodaTime}
 *            (支持的时间单位有DAY,HOUR,MINUTE,SECOND,MILLI)
 */
public static long lengthBetween(DateTime time1, DateTime time2,
		DurationFieldType timeUnit) {
	Duration duration = Days.daysBetween(time1, time2).toStandardDuration();
	if (timeUnit == JodaTime.DAY) {
		return duration.getStandardDays();
	} else if (timeUnit == JodaTime.HOUR) {
		return duration.getStandardHours();
	} else if (timeUnit == JodaTime.MINUTE) {
		return duration.getStandardMinutes();
	} else if (timeUnit == JodaTime.SECOND) {
		return duration.getStandardSeconds();
	} else if (timeUnit == JodaTime.MILLI) {
		return duration.getMillis();
	} else {
		throw new RuntimeException(
				"TimeUnit not supported except DAY,HOUR,MINUTE,SECOND,MILLI");
	}
}
 
Example 2
Source File: ConfigSupport.java    From monsoon with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Convert duration to an representation accepted by Configuration parser.
 * @param duration A duration.
 * @return A StringBuilder with the string representation of the duration.
 */
public static StringBuilder durationConfigString(Duration duration) {
    Duration remainder = duration;

    long days = remainder.getStandardDays();
    remainder = remainder.minus(Duration.standardDays(days));

    long hours = remainder.getStandardHours();
    remainder = remainder.minus(Duration.standardHours(hours));

    long minutes = remainder.getStandardMinutes();
    remainder = remainder.minus(Duration.standardMinutes(minutes));

    long seconds = remainder.getStandardSeconds();
    remainder = remainder.minus(Duration.standardSeconds(seconds));

    if (!remainder.isEqual(Duration.ZERO))
        Logger.getLogger(ConfigSupport.class.getName()).log(Level.WARNING, "Duration is more precise than configuration will handle: {0}, dropping remainder: {1}", new Object[]{duration, remainder});

    StringBuilder result = new StringBuilder();
    if (days != 0) {
        if (result.length() != 0) result.append(' ');
        result.append(days).append('d');
    }
    if (hours != 0) {
        if (result.length() != 0) result.append(' ');
        result.append(hours).append('h');
    }
    if (minutes != 0) {
        if (result.length() != 0) result.append(' ');
        result.append(minutes).append('m');
    }
    if (result.length() == 0 || seconds != 0) {
        if (result.length() != 0) result.append(' ');
        result.append(seconds).append('s');
    }

    return result;
}
 
Example 3
Source File: PluginDateUtils.java    From template-compiler with Apache License 2.0 4 votes vote down vote up
public static void humanizeDate(long instantMs, long baseMs, String tzId, boolean showSeconds, StringBuilder buf) {
  DateTimeZone timeZone = DateTimeZone.forID(tzId);
  int offset = timeZone.getOffset(instantMs);
  Duration delta = new Duration(baseMs - instantMs + offset);

  int days = (int)delta.getStandardDays();
  int years = (int)Math.floor(days / 365.0);
  if (years > 0) {
    humanizeDatePlural(years, DatePartType.YEAR, buf);
    return;
  }
  int months = (int)Math.floor(days / 30.0);
  if (months > 0) {
    humanizeDatePlural(months, DatePartType.MONTH, buf);
    return;
  }
  int weeks = (int)Math.floor(days / 7.0);
  if (weeks > 0) {
    humanizeDatePlural(weeks, DatePartType.WEEK, buf);
    return;
  }
  if (days > 0) {
    humanizeDatePlural(days, DatePartType.DAY, buf);
    return;
  }
  int hours = (int)delta.getStandardHours();
  if (hours > 0) {
    humanizeDatePlural(hours, DatePartType.HOUR, buf);
    return;
  }
  int mins = (int)delta.getStandardMinutes();
  if (mins > 0) {
    humanizeDatePlural(mins, DatePartType.MINUTE, buf);
    return;
  }
  int secs = (int)delta.getStandardSeconds();
  if (showSeconds) {
    humanizeDatePlural(secs, DatePartType.SECOND, buf);
    return;
  }
  buf.append("less than a minute ago");
}