Created
October 21, 2022 11:54
-
-
Save rmg007/14aab52acddf8d33e2ad4c317ac6d5e5 to your computer and use it in GitHub Desktop.
Local Date class
This file contains hidden or 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.*; | |
| import java.time.temporal.ChronoField; | |
| import java.time.temporal.ChronoUnit; | |
| import java.time.temporal.TemporalAdjusters; | |
| public class LocalDateExample { | |
| public static void main(String[] args) { | |
| LocalDate today = LocalDate.now(); | |
| System.out.println("today: " + today); | |
| LocalDate hiringDate = LocalDate.of(2017, Month.DECEMBER, 29); | |
| System.out.println("hiringDate: " + hiringDate); | |
| LocalDate futureDate = LocalDate.parse("2027-12-27"); | |
| System.out.println("futureDate: " + futureDate); | |
| System.out.println(today.getYear()); | |
| System.out.println(today.lengthOfYear()); | |
| System.out.println(today.get(ChronoField.YEAR)); | |
| System.out.println(today.getMonth()); | |
| System.out.println(today.getMonthValue()); | |
| System.out.println(today.lengthOfMonth()); | |
| System.out.println(today.get(ChronoField.MONTH_OF_YEAR)); | |
| System.out.println(today.getDayOfWeek()); | |
| System.out.println(today.get(ChronoField.DAY_OF_WEEK)); | |
| System.out.println(today.getDayOfMonth()); | |
| System.out.println(today.get(ChronoField.DAY_OF_MONTH)); | |
| System.out.println(today.getDayOfYear()); | |
| System.out.println(today.get(ChronoField.DAY_OF_YEAR)); | |
| System.out.println(today.isLeapYear()); | |
| boolean before = today.isBefore(hiringDate); | |
| System.out.println(before); | |
| boolean after = today.isAfter(hiringDate); | |
| System.out.println(after); | |
| LocalDateTime atStartOfDay = today.atTime(9, 15); | |
| atStartOfDay = today.atStartOfDay(); | |
| System.out.println( atStartOfDay); | |
| LocalDate with = today.with(TemporalAdjusters.lastDayOfYear()); | |
| System.out.println(with); | |
| LocalDate with2 = today.with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY)); | |
| System.out.println( with2); | |
| //// plus and minus | |
| today.minusDays(10); | |
| today.minusMonths(2); | |
| today.minusYears(2); | |
| //// number and temporal unit | |
| today.minus(12, ChronoUnit.MONTHS); | |
| //// temporal amount | |
| today.minus(Period.ofDays(12)); | |
| //// years of experience | |
| Period period = Period.between(hiringDate, today); | |
| System.out.println("period " + period); | |
| System.out.println(period.getYears()); | |
| //// years of experience | |
| LocalDate experience = today | |
| .minusYears(hiringDate.getYear()) | |
| .minusMonths(hiringDate.getMonthValue()) | |
| .minusDays(hiringDate.getDayOfMonth()); | |
| System.out.println("experience: " + experience); | |
| System.out.println(LocalDate.MIN); | |
| System.out.println(LocalDate.MAX); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment