/*
* Calculate the span of time from today until your birthday, assuming your
* birthday occured on January 1st. The calculation is done using both
* months and days (using Period) and days only (using ChronoUnit.between).
* 计算距离下一次生日还有多长时间,假设生日在1月1日
* 分别计算了还有几个月零几天和总共还有多少天
*/
import java.time.LocalDate;
import java.time.Month;
import java.time.Period;
import java.time.temporal.ChronoUnit;
import java.io.PrintStream;
public class Birthday {
public static void main(String[] args) {
LocalDate today = LocalDate.now();
LocalDate birthday = LocalDate.of(1960, Month.JANUARY, 1);
LocalDate nextBDay = birthday.withYear(today.getYear());
//If your birthday has occurred this year already, add 1 to the year.
if (nextBDay.isBefore(today) || nextBDay.isEqual(today)) {
nextBDay = nextBDay.plusYears(1);
}
Period p = Period.between(today, nextBDay);
long p2 = ChronoUnit.DAYS.between(today, nextBDay);
System.out.println("There are " + p.getMonths() + " months, and " +
p.getDays() + " days until your next birthday. (" +
p2 + " total)");
}
}