The java.time.temporal.TemporalAdjusters
class is part of new date and time API added in Java 8 which provides common
and useful TemporalAdjusters. TemporalAdjusters class in Java has many static methods for modifying temporal objects
i.e. methods for adjustments of date and time.
You have to understand two interfaces Temporal
and TemporalAdjuster
to understand the
whole concept of temporal objects.
java.time.temporal.Temporal interface
Temporal is a framework-level interface defining read-write access to a temporal object, such as a date, time, offset or some combination of these.
This is the base interface type for date, time and offset objects that are complete enough to be manipulated using plus and minus thus it provides access to date/time/offset information.
Some of the classes in new date and time API that implements Temporal interface are given below-
- Instant
- LocalDate
- LocalDateTime
- LocalTime
- OffsetDateTime
- OffsetTime
- ZonedDateTime
So objects of these classes are of type Temporal and static methods of TemporalAdjusters class can modify these temporal objects.
java.time.temporal.TemporalAdjuster interface
TemporalAdjuster is a functional interface providing strategy for adjusting a temporal object. This interface has a single abstract method adjustInto() which adjusts the specified temporal object.
For example-
temporal = thisAdjuster.adjustInto(temporal);
But the recommended way is to use temporal = temporal.with(thisAdjuster);
method rather than directly
using the adjustInto() method.
java.time.temporal.TemporalAdjusters class
TemporalAdjusters class has many convenient methods that return a TemporalAdjuster. Some of the methods are listed below-
- TemporalAdjuster dayOfWeekInMonth(int ordinal, DayOfWeek dayOfWeek)- Returns the day-of-week in month adjuster, which returns a new date with the ordinal day-of-week based on the month.
- TemporalAdjuster firstDayOfMonth()- Returns the "first day of month" adjuster, which returns a new date set to the first day of the current month.
- TemporalAdjuster firstDayOfNextMonth()- Returns the "first day of next month" adjuster, which returns a new date set to the first day of the next month.
- TemporalAdjuster firstDayOfYear()- Returns the "first day of year" adjuster, which returns a new date set to the first day of the current year.
- TemporalAdjuster lastDayOfMonth()- Returns the "last day of month" adjuster, which returns a new date set to the last day of the current month.
- TemporalAdjuster lastDayOfYear()- Returns the "last day of year" adjuster, which returns a new date set to the last day of the current year.
Java TemporalAdjusters Examples
1. Finding the first or last day of the month for a LocalDateTime object.
LocalDateTime ldt = LocalDateTime.now(); System.out.println("Current date time- " + ldt); System.out.println("First day of the month- " + ldt.with(TemporalAdjusters.firstDayOfMonth())); System.out.println("Last day of the month - " + ldt.with(TemporalAdjusters.lastDayOfMonth()));Output
Current date time- 2019-11-20T10:39:26.583287900 First day of the month- 2019-11-01T10:39:26.583287900 Last day of the month – 2019-11-30T10:39:26.583287900
2. Finding the day of week in the month for a LocalDate object. In the example code first Sunday of the given month and the previous Sunday is calculated.
LocalDate ld = LocalDate.now(); System.out.println("Current Date - " + ld); System.out.println("First Sunday of the month - " + ld.with(TemporalAdjusters.dayOfWeekInMonth(1, DayOfWeek.SUNDAY))); System.out.println("Previous Sunday - " + ld.with(TemporalAdjusters.previous(DayOfWeek.SUNDAY)));Output
Current Date - 2019-11-20 First Sunday of the month - 2019-11-03 Previous Sunday – 2019-11-17
Custom TemporalAdjuster examples
You can implement your own TemporalAdjuster to cater to specific requirement. Suppose you want to get the last working day of the month.
import java.time.DayOfWeek; import java.time.LocalDateTime; import java.time.temporal.Temporal; import java.time.temporal.TemporalAdjuster; import java.time.temporal.TemporalAdjusters; public class WorkingDayAdjuster implements TemporalAdjuster { @Override public Temporal adjustInto(Temporal temporal) { LocalDateTime ldt = LocalDateTime.from(temporal); ldt = ldt.with(TemporalAdjusters.lastDayOfMonth()); if(ldt.getDayOfWeek() == DayOfWeek.SATURDAY || ldt.getDayOfWeek() == DayOfWeek.SUNDAY) { ldt = ldt.with(TemporalAdjusters.previous(DayOfWeek.FRIDAY)); } return temporal.with(ldt); } }
public class WorkingDay { public static void main(String[] args) { LocalDateTime ldt = LocalDateTime.now(); System.out.println("Current Date - " + ldt); System.out.println("Last working day of the month - " + ldt.with(new WorkingDayAdjuster())); } }Output
Current Date - 2019-11-20T11:10:48.365786300 Last working day of the month - 2019-11-29T11:10:48.365786300
That's all for the topic Java TemporalAdjusters Class With Examples. If something is missing or you have something to share about the topic please write a comment.
You may also like
No comments:
Post a Comment