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

84. Calculating the middle of the month

Let’s imagine that we have a LocalDate and we want to calculate from it another LocalDate representing the middle of the month. This can be achieved in seconds if we know that the LocalDate API has a method named lengthOfMonth(), which returns an integer representing the length of the month in days. So, all we have to do is calculate lengthOfMonth()/2 as in the following code:

public static LocalDate middleOfTheMonth(LocalDate date) {
  return LocalDate.of(date.getYear(), date.getMonth(), 
    date.lengthOfMonth() / 2); 
}

In the bundled code, you can see a solution based on the Calendar API.

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