• Javascript
  • Python
  • Go

Getting Last Friday of Month in Java

In the world of programming, Java has become one of the most widely used languages for its versatility and robustness. However, even experie...

In the world of programming, Java has become one of the most widely used languages for its versatility and robustness. However, even experienced Java developers can sometimes struggle with certain tasks, such as getting the last Friday of the month. In this article, we will explore different approaches to achieving this task in Java.

Firstly, let's clarify what we mean by the "last Friday of the month." This refers to the last occurrence of a Friday in a given month. For example, if the month is January and the last Friday falls on the 31st, then that is considered the last Friday of the month. Similarly, if the month is February and the last Friday falls on the 28th or 29th, then that is the last Friday of the month.

Now, let's look at some ways to get the last Friday of the month in Java.

1) Using the Calendar class

The Calendar class in Java provides methods to manipulate dates and times. We can leverage this class to get the last Friday of the month. Here's how:

a) Get the current date and time using the getInstance() method of the Calendar class.

b) Set the date to the last day of the month using the set() method.

c) Use the get() method to retrieve the day of the week for the set date.

d) If it is a Friday (Calendar.FRIDAY), then we have our last Friday of the month. Otherwise, we need to keep subtracting one day from the date until we get a Friday.

Here's an example code snippet:

```

Calendar calendar = Calendar.getInstance();

calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));

while(calendar.get(Calendar.DAY_OF_WEEK) != Calendar.FRIDAY){

calendar.add(Calendar.DAY_OF_MONTH, -1);

}

Date lastFriday = calendar.getTime();

```

2) Using the LocalDate class

Java 8 introduced the LocalDate class, which provides a more streamlined approach to working with dates. Here's how we can use this class to get the last Friday of the month:

a) Get the last day of the month using the with() method.

b) Use the with() method again to set the day of the week to Friday.

c) If the day of the week is not Friday, then we need to subtract one week until we get a Friday.

Here's an example code snippet:

```

LocalDate currentDate = LocalDate.now();

LocalDate lastDayOfMonth = currentDate.with(TemporalAdjusters.lastDayOfMonth());

LocalDate lastFriday = lastDayOfMonth.with(TemporalAdjusters.previousOrSame(DayOfWeek.FRIDAY));

if(lastFriday.getMonth() != currentDate.getMonth()){

lastFriday = lastFriday.minusWeeks(1);

}

```

3) Using the java.time.DayOfWeek enum

Another approach is to use the DayOfWeek enum from the java.time package. This enum provides a method called lastInMonth() which returns the last occurrence of a given day of the week in a month. Here's how we can use this method to get the last Friday of the month:

```

LocalDate currentDate = LocalDate.now();

LocalDate lastFriday = currentDate.with(TemporalAdjusters.dayOfWeekInMonth(0, DayOfWeek.FRIDAY));

```

In this approach, we are setting the count parameter to 0, which means we are looking for the last occurrence of the given day of the week in the month.

In conclusion, getting the last Friday of the month in Java can be achieved in multiple ways. We have explored three different approaches using the Calendar class, the LocalDate class, and the java.time.DayOfWeek enum. As a developer, it is essential to understand the different options available and choose the one that best suits your project and coding style. Happy coding!

Related Articles

Business Day Calculator

As a business owner, it is crucial to stay on top of your finances and manage your time effectively. One tool that can help with this is a b...