本文整理汇总了Java中java.time.Period.between方法的典型用法代码示例。如果您正苦于以下问题:Java Period.between方法的具体用法?Java Period.between怎么用?Java Period.between使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.time.Period
的用法示例。
在下文中一共展示了Period.between方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: factory_between_LocalDate
import java.time.Period; //导入方法依赖的package包/类
@Test(dataProvider="between")
public void factory_between_LocalDate(int y1, int m1, int d1, int y2, int m2, int d2, int ye, int me, int de) {
LocalDate start = LocalDate.of(y1, m1, d1);
LocalDate end = LocalDate.of(y2, m2, d2);
Period test = Period.between(start, end);
assertPeriod(test, ye, me, de);
//assertEquals(start.plus(test), end);
}
示例2: getAge
import java.time.Period; //导入方法依赖的package包/类
public static int getAge(String personalCode) {
LocalDate today = LocalDate.now();
LocalDate birthDate = getBirthDate(personalCode);
Period p = Period.between(birthDate, today);
return p.getYears();
}
示例3: main
import java.time.Period; //导入方法依赖的package包/类
public static void main(String[] args) {
LocalDate date1 = LocalDate.of(2015, 10, 13);
LocalDate date2 = LocalDate.now();
Period period1 = Period.between(date1, date2);
System.out.println("Date1: " + date1);
System.out.println("Date2: " + date2);
System.out.println("Periode (date1 to date2): " + period1);
System.out.println("\tYears: " + period1.getYears());
System.out.println("\tMonths: " + period1.getMonths());
System.out.println("\tDays: " + period1.getDays());
}
示例4: formatLongDuration
import java.time.Period; //导入方法依赖的package包/类
public static String formatLongDuration(Instant instant) {
Period period = Period.between(TimeUtils.toLocalDate(instant).toLocalDate(), LocalDate.now());
String str = period.getUnits().stream()
.filter(unit -> period.get(unit) != 0)
.map(unit -> String.format("%d %s", period.get(unit), unit.toString().toLowerCase()))
.collect(Collectors.joining(", "));
return str.isEmpty() ? FormatUtils.formatShortDuration(instant.toEpochMilli()) : str;
}
示例5: 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)"));
});
}
示例6: calculateAge
import java.time.Period; //导入方法依赖的package包/类
public String calculateAge(LocalDate birthday) {
String age = null;
if (null != birthday) {
Period period = Period.between(birthday, now);
age = String.valueOf(period.getYears());
}
return age;
}
示例7: getDateDiff
import java.time.Period; //导入方法依赖的package包/类
private DateCalculatorResult getDateDiff(final DateToken startDateToken, final Token thirdToken) {
LocalDate one = startDateToken.getValue();
LocalDate two = ((DateToken) thirdToken).getValue();
return (one.isBefore(two))
? new DateCalculatorResult(Period.between(one, two))
: new DateCalculatorResult(Period.between(two, one));
}
示例8: main
import java.time.Period; //导入方法依赖的package包/类
public static void main(String[] args) {
Period period = Period.of(1, 2, 7);
Period period2 = Period.ofYears(2);
Period period3 = Period.ofMonths(5);
Period period4 = Period.ofWeeks(10);
Period period5 = Period.ofDays(15);
Period period6 = Period.ofDays(15);
Period p5Yrs1 = Period.parse("P5y");
Period p5Yrs2 = Period.parse("p5y");
Period p5Yrs3 = Period.parse("P5Y");
Period p5Yrs4 = Period.parse("+P5Y");
Period p5Yrs5 = Period.parse("P+5Y");
Period p5Yrs6 = Period.parse("-P-5Y");
System.out.printf("%s : %s", p5Yrs1, p5Yrs2);
System.out.println();
System.out.printf("-P-5Y: %s", p5Yrs6);
System.out.println();
Period p5Yrs7 = Period.parse("P5y1m2d");
Period p5Yrs8 = Period.parse("p9m");
Period p5Yrs9 = Period.parse("P60d");
// For the string form PnW, the count of weeks is multiplied by 7 to
// get the number of days
Period p5Yrs10 = Period.parse("-P5Y5W");
System.out.printf("p5Yrs7: %s, p5Yrs8: %s, p5Yrs9: %s, p5Yrs10: %s",
p5Yrs7,
p5Yrs8,
p5Yrs9,
p5Yrs10);
System.out.println();
// static method between
LocalDate carnivalStart = LocalDate.of(2050, 12, 31);
LocalDate carnivalEnd = LocalDate.of(2051, 1, 2);
// period = endDate - startDate
Period periodBetween = Period.between(carnivalEnd, carnivalStart);
System.out.println(periodBetween );
}
示例9: factory_between_LocalDate_nullFirst
import java.time.Period; //导入方法依赖的package包/类
@Test(expectedExceptions=NullPointerException.class)
public void factory_between_LocalDate_nullFirst() {
Period.between((LocalDate) null, LocalDate.of(2010, 1, 1));
}
示例10: factory_between_LocalDate_nullSecond
import java.time.Period; //导入方法依赖的package包/类
@Test(expectedExceptions=NullPointerException.class)
public void factory_between_LocalDate_nullSecond() {
Period.between(LocalDate.of(2010, 1, 1), (LocalDate) null);
}