concept TemporalAdjusters in category java

appears as: TemporalAdjusters, TemporalAdjusters
Modern Java in Action: Lambdas, streams, reactive and functional programming

This is an excerpt from Manning's book Modern Java in Action: Lambdas, streams, reactive and functional programming.

Table 12.3 lists the TemporalAdjusters that you can create with these factory methods.

Table 12.3. The factory methods of the TemporalAdjusters class

Method

Description

dayOfWeekInMonth Creates a new date in the same month with the ordinal day of week. (Negative numbers count backward from the end of the month.)
firstDayOfMonth Creates a new date set to the first day of the current month.
firstDayOfNextMonth Creates a new date set to the first day of the next month.
firstDayOfNextYear Creates a new date set to the first day of the next year.
firstDayOfYear Creates a new date set to the first day of the current year.
firstInMonth Creates a new date in the same month with the first matching day of the week.
lastDayOfMonth Creates a new date set to the last day of the current month.
lastDayOfNextMonth Creates a new date set to the last day of the next month.
lastDayOfNextYear Creates a new date set to the last day of the next year.
lastDayOfYear Creates a new date set to the last day of the current year.
lastInMonth Creates a new date in the same month with the last matching day of the week.
next previous Creates a new date set to the first occurrence of the specified day of week after/before the date being adjusted.
nextOrSame previousOrSame Creates a new date set to the first occurrence of the specified day of week after/before the date being adjusted unless it’s already on that day, in which case the same object is returned.

As you can see, TemporalAdjusters allow you to perform more-complex date manipulations that still read like the problem statement. Moreover, it’s relatively simple to create your own custom TemporalAdjuster implementation if you can’t find a predefined TemporalAdjuster that fits your needs. In fact, the TemporalAdjuster interface declares only a single method (which makes it a functional interface), defined as shown in the following listing.

Java 8 in Action: Lambdas, streams, and functional-style programming

This is an excerpt from Manning's book Java 8 in Action: Lambdas, streams, and functional-style programming.

Table 12.3 provides a list of the TemporalAdjusters that can be created with these factory methods.

Table 12.3. The factory methods of the TemporalAdjusters class

Method

Description

dayOfWeekInMonth Creates a new date in the same month with the ordinal day of week
firstDayOfMonth Creates a new date set to the first day of the current month
firstDayOfNextMonth Creates a new date set to the first day of the next month
firstDayOfNextYear Creates a new date set to the first day of the next year
firstDayOfYear Creates a new date set to the first day of the current year
firstInMonth Creates a new date in the same month with the first matching day of week
lastDayOfMonth Creates a new date set to the last day of the current month
lastDayOfNextMonth Creates a new date set to the last day of the next month
lastDayOfNextYear Creates a new date set to the last day of the next year
lastDayOfYear Creates a new date set to the last day of the current year
lastInMonth Creates a new date in the same month with the last matching day of week
next previous Creates a new date set to the first occurrence of the specified day of week after/before the date being adjusted
nextOrSame previousOrSame Creates a new date set to the first occurrence of the specified day of week after/before the date being adjusted unless it’s already on that day, in which case the same object is returned

As you can see, TemporalAdjusters allow you to perform more complex date manipulations that still read like the problem statement. Moreover, it’s relatively simple to create your own custom TemporalAdjuster implementation if you can’t find a predefined TemporalAdjuster that fits your need. In fact, the TemporalAdjuster interface declares only a single method (this makes it a functional interface), defined as follows.