Java计算从今天到生日的时间跨度
/* * 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). */ import java.time.LocalDate; import java.time.Month;// from N o w J a v a . c o m 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); /** from N o w J a v a . c o m**/ 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)"); } }