Created
January 25, 2017 18:02
-
-
Save kousen/2cc479c0abf337eaff50bfa6fe0fc6d3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.time.LocalDate; | |
import java.time.Month; | |
import static java.time.temporal.ChronoUnit.DAYS; | |
import static java.time.temporal.ChronoUnit.MONTHS; | |
import static java.time.temporal.ChronoUnit.YEARS; | |
public class DaysToElection { | |
private static String pluralize(long num) { | |
return num == 1 ? "" : "s"; | |
} | |
public static void main(String[] args) { | |
LocalDate electionDay = LocalDate.of(2020, Month.NOVEMBER, 3); | |
LocalDate today = LocalDate.now(); | |
System.out.printf("%d days to go...%n", DAYS.between(today, electionDay)); | |
long years = YEARS.between(today, electionDay); | |
long months = MONTHS.between(today.plusYears(years), electionDay); | |
long days = DAYS.between(today.plusYears(years).plusMonths(months), electionDay); | |
System.out.printf("%d year%s, %d month%s, and %d day%s%n", | |
years, | |
pluralize(years), | |
months, | |
pluralize(months), | |
days, | |
pluralize(days)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment