Oleksandr Gavenko's blog
2017-05-31 23:49 How old am I in java.time

ChronoUnit provides constants with between method. To find own age I use ChronoUnit.YEARS.

With LocalDate:

LocalDate today = LocalDate.now();
LocalDate birthday = LocalDate.of(1984, Month.OCTOBER, 2);
System.out.println(ChronoUnit.YEARS.between(birthday, today));

With LocalDateTime:

LocalDateTime today = LocalDateTime.now();
LocalDateTime birthday = LocalDateTime.of(1984, Month.OCTOBER, 2, 0, 0);
System.out.println(ChronoUnit.YEARS.between(birthday, today));
java

Feeds

all / emacs / java / python

Tags

admin(1), anki(1), blog(1), css(2), cygwin(2), emacs(3), fs(1), git(2), gradle(1), hg(2), html(1), interview(11), java(3), js(3), lang(2), lighttpd(1), mobile(1), naming(1), oracle(1), print(1), problem(5), quiz(6), rst(1), security(1), sql(2), srs(1), style(1), unit(1), utils(1), vcs(3), web(2), win(2)

Archive