Reader small image

You're reading from  Java Coding Problems - Second Edition

Product typeBook
Published inMar 2024
PublisherPackt
ISBN-139781837633944
Edition2nd Edition
Right arrow
Author (1)
Anghel Leonard
Anghel Leonard
author image
Anghel Leonard

Anghel Leonard is a Chief Technology Strategist and independent consultant with 20+ years of experience in the Java ecosystem. In daily work, he is focused on architecting and developing Java distributed applications that empower robust architectures, clean code, and high-performance. Also passionate about coaching, mentoring and technical leadership. He is the author of several books, videos and dozens of articles related to Java technologies.
Read more about Anghel Leonard

Right arrow

83. Getting the first and last day of the week

Let’s assume given an integer (nrOfWeeks) representing the number of weeks that we want to extract the first and last day of each week starting from now. For instance, for the given nrOfWeeks = 3 and a local date such as 06/02/2023, we want this:

[
Mon 06/02/2023,
Sun 12/02/2023,
Mon 13/02/2023,
Sun 19/02/2023,
Mon 20/02/2023,
Sun 26/02/2023
]

This is much easier than it might seem. We just need a loop from 0 to nrOfWeeks and two TemporalAdjusters to fit the first/last day of each week. More precisely, we need the nextOrSame(DayOfWeek dayOfWeek) and previousOrSame(DayOfWeek dayOfWeek) adjusters.

The nextOrSame() adjuster’s role is to adjust the current date to the first occurrence of the given day of week after the date being adjusted (this can be next or same). On the other hand, the previousOrSame() adjuster’s role is to adjust the current date to the first occurrence of the given day of week before...

lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Java Coding Problems - Second Edition
Published in: Mar 2024Publisher: PacktISBN-13: 9781837633944

Author (1)

author image
Anghel Leonard

Anghel Leonard is a Chief Technology Strategist and independent consultant with 20+ years of experience in the Java ecosystem. In daily work, he is focused on architecting and developing Java distributed applications that empower robust architectures, clean code, and high-performance. Also passionate about coaching, mentoring and technical leadership. He is the author of several books, videos and dozens of articles related to Java technologies.
Read more about Anghel Leonard