Skip to content

Instantly share code, notes, and snippets.

@kousen
Created January 25, 2017 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kousen/2cc479c0abf337eaff50bfa6fe0fc6d3 to your computer and use it in GitHub Desktop.
Save kousen/2cc479c0abf337eaff50bfa6fe0fc6d3 to your computer and use it in GitHub Desktop.
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