September 2019
Intermediate to advanced
816 pages
18h 47m
English
For a Date object, the solution may rely on a Calendar instance. The code that is bundled to this book contains this solution.
For JDK 8 classes, Java provides dedicated getFoo() methods and a get(TemporalField field) method. For example, let's assume the following LocalDateTime object:
LocalDateTime ldt = LocalDateTime.now();
Relying on getFoo() methods, we get the following code:
int year = ldt.getYear();int month = ldt.getMonthValue();int day = ldt.getDayOfMonth();int hour = ldt.getHour();int minute = ldt.getMinute();int second = ldt.getSecond();int nano = ldt.getNano();
Or, relying on get(TemporalField field) results in the following:
int yearLDT = ldt.get(ChronoField.YEAR);int monthLDT = ldt.get(ChronoField.MONTH_OF_YEAR); ...