Skip to content

Instantly share code, notes, and snippets.

@juanpabloprado
Created April 27, 2019 20:48
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 juanpabloprado/0ad31a0f78fceccd9f3eb6e1383690e5 to your computer and use it in GitHub Desktop.
Save juanpabloprado/0ad31a0f78fceccd9f3eb6e1383690e5 to your computer and use it in GitHub Desktop.
Period plus/minus methods on the Java Shell tool
jshell> Period period = Period.of(5, 2, 1);
period ==> P5Y2M1D
jshell> period = period.plusYears(10);
period ==> P15Y2M1D
jshell> period = period.plusMonths(10);
period ==> P15Y12M1D
jshell> period = period.plusDays(15);
period ==> P15Y12M16D
jshell> period = period.plus(Period.ofDays(15));
period ==> P15Y12M31D
jshell> // Plus a total 10 years, 10 months and 30 days
jshell> "Period value: " + period
$16 ==> "Period value: P15Y12M31D"
jshell> Period period = Period.of(15, 12, 31);
period ==> P15Y12M31D
jshell> period = period.minusYears(10);
period ==> P5Y12M31D
jshell> period = period.minusMonths(10);
period ==> P5Y2M31D
jshell> period = period.minusDays(15);
period ==> P5Y2M16D
jshell> period = period.minus(Period.ofDays(15));
period ==> P5Y2M1D
jshell> // Minused a total 10 years, 10 months and 30 days
jshell> "Period value: " + period;
$22 ==> "Period value: P5Y2M1D"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment