本文整理匯總了Java中java.time.Period.getMonths方法的典型用法代碼示例。如果您正苦於以下問題:Java Period.getMonths方法的具體用法?Java Period.getMonths怎麽用?Java Period.getMonths使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.time.Period
的用法示例。
在下文中一共展示了Period.getMonths方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: processPeriod
import java.time.Period; //導入方法依賴的package包/類
private String processPeriod(Period p) {
long years = p.getYears();
long months = p.getMonths();
long days = p.getDays();
String result = "";
if (years > 0) {
result += years + " years, ";
}
if (months > 0) {
result += months + " months, ";
}
if (days > 0) {
result += days + " days";
}
return result;
}
示例2: timeSince
import java.time.Period; //導入方法依賴的package包/類
private static Function<Instant, Text> timeSince(final Locale locale) {
return (instant -> {
Text text = Text.of(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM)
.withLocale(locale)
.withZone(ZoneId.systemDefault())
.format(instant));
String str = instant.compareTo(Instant.now()) < 0 ? " (%s %s from now)" : " (%s %s ago)";
Duration dur = Duration.between(instant, Instant.now());
if (dur.getSeconds() < 1)
return text.concat(Text.of(" (Now)"));
// seconds
if (dur.getSeconds() < 60)
return text.concat(Text.of(String.format(str, dur.getSeconds(), "seconds")));
// minutes
if (dur.toMinutes() < 60)
return text.concat(Text.of(" (" + dur.toMinutes() + " minutes ago)"));
// hours
if (dur.toHours() < 24)
return text.concat(Text.of(" (" + dur.toHours() + " hours ago)"));
// days
if (dur.toDays() < 365)
return text.concat(Text.of(" (" + dur.toDays() + " days ago)"));
// Duration doesn't support months or years
Period per = Period.between(LocalDate.from(instant), LocalDate.now());
// months
if (per.getMonths() < 12) {
return text.concat(Text.of(" (" + per.getMonths() + " months ago)"));
}
// years
return text.concat(Text.of(" (" + per.getYears() + " years ago)"));
});
}