下面列出了org.joda.time.Duration#getStandardDays ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 获得两个时间点之间的时间跨度
*
* @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");
}
}
/**
* 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;
}
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");
}