[Java1.8 +] Get the date of the next x day of the week with LocalDate

The TemporalAdjuster returned by the next (DayOfWeek dayOfWeek) method of the TemporalAdjusters You can change the date of LocalDate to the date of the next x day using an instance of: //docs.oracle.com/javase/jp/8/docs/api/java/time/temporal/TemporalAdjuster.html) ..

Sample code

LocalDate d = LocalDate.of(2019, 3, 25).with(TemporalAdjusters.next(DayOfWeek.MONDAY));
LocalDate d2 = LocalDate.of(2019, 3, 29).with(TemporalAdjusters.next(DayOfWeek.valueOf("MONDAY")));
LocalDate d3 = LocalDate.of(2019, 3, 31).with(TemporalAdjusters.next(DayOfWeek.of(1)));

 System.out.println(d.toString());
 System.out.println(d2.toString());
 System.out.println(d3.toString());

output

2019-04-01
2019-04-01
2019-04-01

If the LocalDate date is already that day of the week, it will be set to the next week's date.

Using nextOrSame (DayOfWeek dayOfWeek) does nothing if LocalDate is already that day of the week.

sample

LocalDate d = LocalDate.of(2019, 3, 25).with(TemporalAdjusters.nextOrSame(DayOfWeek.MONDAY));
LocalDate d2 = LocalDate.of(2019, 3, 29).with(TemporalAdjusters.nextOrSame(DayOfWeek.valueOf("MONDAY")));
LocalDate d3 = LocalDate.of(2019, 3, 31).with(TemporalAdjusters.nextOrSame(DayOfWeek.of(1)));

output

2019-03-25
2019-04-01
2019-04-01

Recommended Posts

[Java1.8 +] Get the date of the next x day of the week with LocalDate
[Java] Get the day of the specific day of the week
Get the next business day after the specified date in JAVA
[Java] Get the date with the LocalDateTime class
How to get today's day of the week
[Ubuntu 20.04] Display the day of the week on the date / clock
[Java] Get the date 10 days later with the Calendar class
Output of the day of the week using Ruby's Date class
[Java] Calculate the day of the week from the date (Calendar class is not used)
[Java] How to display the day of the week acquired by LocalDate, DateTimeformatter in Japanese
[Ruby] Display today's day of the week using Date class
Get the result of POST in Java
[day: 5] I summarized the basics of Java
How to get the date in java
Format the contents of LocalDate with DateTimeFormatter
Memo: [Java] Identify files with an update date of 1 day or less
[Java] Get MimeType from the contents of the file with Apathce Tika [Kotlin]
Zeller's official (asking for the day of the week)
I want to output the day of the week
[Java] Get the length of the surrogate pair string
Calculate the similarity score of strings with JAVA
[Ruby] Code to display the day of the week
The date time of java8 has been updated
[Java] How to get the authority of the folder
Java unit test Judgment of specified day of the week Determines whether ArDate, Date, Calendar, and long are specified days of the week.
[Java] How to get the URL of the transition source
Get the URL of the HTTP redirect destination in Java
[Swift] Get the number of steps with CMP edometer
CI the architecture of Java / Kotlin applications with ArchUnit
[Java] How to get the maximum value of HashMap
Monitor the internal state of Java programs with Kubernetes
Check the behavior of Java Intrinsic Locks with bpftrace
[Java] Get the file in the jar regardless of the environment
[Java] Get the file path in the folder with List
The story of making dto, dao-like with java, sqlite
Replace only part of the URL host with java
Get started with serverless Java with the lightweight framework Micronaut!
Was done in the base year of the Java calendar week
Get to the abbreviations from 5 examples of iterating Java lists
[Java] Simplify the implementation of data history management with Reladomo
How to derive the last day of the month in Java
Be sure to compare the result of Java compareTo with 0
Conversion of String, Date, LocalDate
4th day of java learning
Java is the 5th day
[Java] Get date information 10 days later using milliseconds in the Date class
[Java] Get the dates of the past Monday and Sunday in order
Try Hello World with the minimum configuration of Heroku Java spring-boot
A story about hitting the League Of Legends API with JAVA
Get the public URL of a private Flickr file in Java
Change the conversation depending on which day of the week is today
The point of addiction when performing basic authentication with Java URLConnection
Overwrite upload of file with the same name with BOX SDK (java)
Is the version of Elasticsearch you are using compatible with Java 11?
How to get the length of an audio file in java
[Java] Delete the elements of List
[Java version] The story of serialization
About Lambda, Stream, LocalDate of Java8
Date processing in Java (LocalDate: Initialization)
[Android] Get the date on Monday
Follow the link with Selenium (Java)