How to Use TemporalAdjusters in Java
Tags: TemporalAdjusters TemporalAdjuster
In this Java core tutorial, we learn how to use the java.time.temporal.TemporalAdjusters class which provides different java.time.temporal.TemporalAdjuster values to adjust date in Java programming language.
How to get first day of month
In the following Java program, we learn how to use the TemporalAdjusters.firstDayOfMonth() adjuster to get the first day of month from a given LocalDate object.
TemporalAdjustersExample1.java
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
public class TemporalAdjustersExample1 {
public static void main(String... args) {
LocalDate localDate = LocalDate.of(2022, 2, 3);
// get first day of month
LocalDate firstDayOfMonth = localDate.with(TemporalAdjusters.firstDayOfMonth());
System.out.println("Date: " + localDate);
System.out.println("First date of month: " + firstDayOfMonth);
}
}
Date: 2022-02-03
First date of month: 2022-02-01
How to get last day of month
With the TemporalAdjusters.lastDayOfMonth() adjuster we can get the last day of month from a given LocalDate object as the following example Java code.
TemporalAdjustersExample2.java
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
public class TemporalAdjustersExample2 {
public static void main(String... args) {
LocalDate localDate = LocalDate.of(2022, 2, 3);
// get last day of month
LocalDate lastDayOfMonth = localDate.with(TemporalAdjusters.lastDayOfMonth());
System.out.println("Date: " + localDate);
System.out.println("Last date of month: " + lastDayOfMonth);
}
}
Date: 2022-02-03
Last date of month: 2022-02-28
How to get first day of next month
With a given LocalDate object, we can use the TemporalAdjusters.firstDayOfNextMonth() adjuster to get the first day of next month as example Java code below.
TemporalAdjustersExample3.java
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
public class TemporalAdjustersExample3 {
public static void main(String... args) {
LocalDate localDate = LocalDate.of(2022, 2, 15);
// get first day of next month
LocalDate firstDayOfNextMonth = localDate.with(TemporalAdjusters.firstDayOfNextMonth());
System.out.println("Date: " + localDate);
System.out.println("First date of next month: " + firstDayOfNextMonth);
}
}
Date: 2022-02-15
First date of next month: 2022-03-01
How to get first day of year
In the following Java program, we use the TemporalAdjusters.firstDayOfYear() adjuster to get the first day of year from a given LocalDate object.
TemporalAdjustersExample4.java
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
public class TemporalAdjustersExample4 {
public static void main(String... args) {
LocalDate localDate = LocalDate.of(2022, 7, 11);
// get first day of year
LocalDate firstDayOfYear = localDate.with(TemporalAdjusters.firstDayOfYear());
System.out.println("Date: " + localDate);
System.out.println("First date of year: " + firstDayOfYear);
}
}
Date: 2022-07-11
First date of year: 2022-01-01
How to get last day of year
In the following Java program, we use the TemporalAdjusters.lastDayOfYear() adjuster to get the last day of year from a given LocalDate object.
TemporalAdjustersExample5.java
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
public class TemporalAdjustersExample5 {
public static void main(String... args) {
LocalDate localDate = LocalDate.of(2022, 7, 23);
// get last day of year
LocalDate lastDayOfYear = localDate.with(TemporalAdjusters.lastDayOfYear());
System.out.println("Date: " + localDate);
System.out.println("Last date of year: " + lastDayOfYear);
}
}
Date: 2022-07-23
Last date of year: 2022-12-31
How to get first day of next year
In the following Java program, we use the TemporalAdjusters.firstDayOfNextYear() adjuster to get the first day of next year from a given LocalDate object.
TemporalAdjustersExample6.java
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
public class TemporalAdjustersExample6 {
public static void main(String... args) {
LocalDate localDate = LocalDate.of(2022, 4, 30);
// get first day of next year
LocalDate firstDayOfNextYear = localDate.with(TemporalAdjusters.firstDayOfNextYear());
System.out.println("Date: " + localDate);
System.out.println("First date of next year: " + firstDayOfNextYear);
}
}
Date: 2022-04-30
First date of next year: 2023-01-01
How to get first specified day of week in month
With the TemporalAdjusters.firstInMonth(DayOfWeek dayOfWeek) adjuster, we can get the first specified day of week in month from a given LocalDate object. For example, in the following Java code we learn how to get the first Monday in month from a given LocalDate object.
TemporalAdjustersExample7.java
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
public class TemporalAdjustersExample7 {
public static void main(String... args) {
LocalDate localDate = LocalDate.of(2022, 4, 30);
// get first Monday in month
LocalDate firstMondayInMonth = localDate.with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
System.out.println("Date: " + localDate);
System.out.println("First Monday in month: " + firstMondayInMonth);
}
}
Date: 2022-04-30
First Monday in month: 2022-04-04
How to get last specified day of week in month
With the TemporalAdjusters.lastInMonth(DayOfWeek dayOfWeek) adjuster, we can get the last specified day of week in month from a given LocalDate object. For example, in the following Java code we learn how to get the last Friday in month from a given LocalDate object.
TemporalAdjustersExample8.java
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
public class TemporalAdjustersExample8 {
public static void main(String... args) {
LocalDate localDate = LocalDate.of(2022, 4, 30);
// get last Friday in month
LocalDate lastFridayInMonth = localDate.with(TemporalAdjusters.lastInMonth(DayOfWeek.FRIDAY));
System.out.println("Date: " + localDate);
System.out.println("Last Friday in month: " + lastFridayInMonth);
}
}
Date: 2022-04-30
Last Friday in month: 2022-04-29
How to get specified day of week based on ordinal in month
With the TemporalAdjusters.dayOfWeekInMonth(int ordinal, DayOfWeek dayOfWeek) adjuster, we can get the specified day of week with the ordinal in month from a given LocalDate object. For example, we can get the second Tuesday in month as the following Java code.
TemporalAdjustersExample9.java
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
public class TemporalAdjustersExample9 {
public static void main(String... args) {
LocalDate localDate = LocalDate.of(2022, 4, 30);
// get second Tuesday in month
LocalDate secondTuesdayInMonth = localDate.with(TemporalAdjusters.dayOfWeekInMonth(2, DayOfWeek.TUESDAY));
System.out.println("Date: " + localDate);
System.out.println("Second Tuesday in month: " + secondTuesdayInMonth);
}
}
Date: 2022-04-30
Second Tuesday in month: 2022-04-12
How to get next specified day of week
With the TemporalAdjusters.next(DayOfWeek dayOfWeek) adjuster, we can get the next specified day of week from a given LocalDate object. For example, we can get the next Monday as the following example Java code.
TemporalAdjustersExample10.java
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
public class TemporalAdjustersExample10 {
public static void main(String... args) {
LocalDate localDate = LocalDate.of(2022, 9, 6);
// get next Monday
LocalDate nextMonday = localDate.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
System.out.println("Date: " + localDate);
System.out.println("Next Monday: " + nextMonday);
}
}
Date: 2022-09-06
Next Monday: 2022-09-12
How to get next specified day of week or the same
With the TemporalAdjusters.nextOrSame(DayOfWeek dayOfWeek) adjuster, we can get the next specified day of week or return the same date if it is the same day of week as the following example Java code.
TemporalAdjustersExample11.java
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
public class TemporalAdjustersExample11 {
public static void main(String... args) {
LocalDate localDate = LocalDate.of(2022, 9, 6);
// get next Tuesday or same
LocalDate nextTuesdayOrSame = localDate.with(TemporalAdjusters.nextOrSame(DayOfWeek.TUESDAY));
System.out.println("Date: " + localDate);
System.out.println("Next Tuesday or same: " + nextTuesdayOrSame);
}
}
Date: 2022-09-06
Next Tuesday or same: 2022-09-06
How to get previous specified day of week
With the TemporalAdjusters.previous(DayOfWeek dayOfWeek) adjuster, we can get the previous specified day of week from a given LocalDate object. For example, we can get the previous Monday as the following example Java code.
TemporalAdjustersExample12.java
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
public class TemporalAdjustersExample12 {
public static void main(String... args) {
LocalDate localDate = LocalDate.of(2022, 9, 5);
// get previous Monday
LocalDate previousMonday = localDate.with(TemporalAdjusters.previous(DayOfWeek.MONDAY));
System.out.println("Date: " + localDate);
System.out.println("Previous Monday: " + previousMonday);
}
}
Date: 2022-09-05
Previous Monday: 2022-08-29
How to get previuous specified day of week or the same
With the TemporalAdjusters.previousOrSame(DayOfWeek dayOfWeek) adjuster, we can get the previous specified day of week or return the same date if it is the same day of week as the following example Java code.
TemporalAdjustersExample13.java
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
public class TemporalAdjustersExample13 {
public static void main(String... args) {
LocalDate localDate = LocalDate.of(2022, 9, 5);
// get previous Monday or same
LocalDate previousMondayOrSame = localDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
System.out.println("Date: " + localDate);
System.out.println("Previous Monday or same: " + previousMondayOrSame);
}
}
Date: 2022-09-05
Previous Monday or same: 2022-09-05
How to create custom TemporalAdjuster
With the TemporalAdjusters.ofDateAdjuster(UnaryOperator
TemporalAdjustersExample14.java
import java.time.LocalDate;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
public class TemporalAdjustersExample14 {
public static void main(String... args) {
final TemporalAdjuster TEN_DAY_LATER = TemporalAdjusters.ofDateAdjuster(localDate -> localDate.plusDays(10));
LocalDate localDate = LocalDate.of(2022, 9, 5);
// get ten days later
LocalDate tenDayLater = localDate.with(TEN_DAY_LATER);
System.out.println("Date: " + localDate);
System.out.println("Ten days later: " + tenDayLater);
}
}
Date: 2022-09-05
Ten days later: 2022-09-15
Happy Coding 😊
Related Articles
How to Sort a List of Date in Java
Java Add Number of Days to Current Date
Java Add Number of Days to Current LocalDate
Java Add Number of Days to Current LocalDateTime