How do I use firstInMonth() and lastInMonth() method of TemporalAdjusters class?

TemporalAdjusters.firstInMonth(DayOfWeek) and TemporalAdjusters.lastInMonth(DayOfWeek) methods are part of the java.time.temporal.TemporalAdjusters class. They adjust the date to the first or last occurrence of the specified DayOfWeek in the month.

Here’s an example of how to use these methods:

package org.kodejava.datetime;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;

public class FirstLastInMonthExample {
    public static void main(String[] args) {
        // Get the current date
        LocalDate date = LocalDate.now();
        System.out.println("Current date: " + date);

        LocalDate firstMondayInMonth = date.with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
        System.out.println("First Monday of this month: " + firstMondayInMonth);

        LocalDate lastFridayInMonth = date.with(TemporalAdjusters.lastInMonth(DayOfWeek.FRIDAY));
        System.out.println("Last Friday of this month: " + lastFridayInMonth);
    }
}

The output of the code snippet above:

Current date: 2024-01-18
First Monday of this month: 2024-01-01
Last Friday of this month: 2024-01-26

In this example:

  • LocalDate.now()` is used to get the current date.
  • .with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY)) adjusts the date to the first Monday of the current month.
  • .with(TemporalAdjusters.lastInMonth(DayOfWeek.FRIDAY)) adjusts the date to the last Friday of the current month.

How do I use firstDayOfYear() and firstDayOfNextYear() method of TemporalAdjusters class?

The TemporalAdjusters.firstDayOfYear() and TemporalAdjusters.firstDayOfNextYear() methods in Java are utilized to adjust a date to the first day of the current year and the first day of the next year respectively.

Here’s how to use these methods:

package org.kodejava.datetime;

import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;

public class FirstDayOfYearExample {
    public static void main(String[] args) {
        // Det the current date
        LocalDate date = LocalDate.now();
        System.out.println("Current date: " + date);

        LocalDate firstDayOfYear = date.with(TemporalAdjusters.firstDayOfYear());
        System.out.println("First day of this year: " + firstDayOfYear);

        LocalDate firstDayOfNextYear = date.with(TemporalAdjusters.firstDayOfNextYear());
        System.out.println("First day of next year: " + firstDayOfNextYear);
    }
}

In this example:

  • LocalDate.now() is used to get the current date.
  • .with(TemporalAdjusters.firstDayOfYear()) adjusts the date to the first day of the current year.
  • .with(TemporalAdjusters.firstDayOfNextYear()) adjusts the date to the first day of the next year.

The output of the code snippet above:

Current date: 2024-01-18
First day of this year: 2024-01-01
First day of next year: 2025-01-01

How do I use TemporalAdjusters firstDayOfNextMonth() method?

The TemporalAdjusters.firstDayOfNextMonth() method is a useful method in java.time.temporal.TemporalAdjusters class in Java that adjusts the date to the first day of the next month.

Here’s an example usage:

package org.kodejava.datetime;

import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;

public class FirstDayOfNextMonthExample {
    public static void main(String[] args) {
        // Get the current date
        LocalDate date = LocalDate.now();

        // Adjust to the first day of next month
        LocalDate firstDayOfNextMonth = date.with(TemporalAdjusters.firstDayOfNextMonth());

        System.out.println("Current date: " + date);
        System.out.println("First day of next month: " + firstDayOfNextMonth);
    }
}

Output:

Current date: 2024-01-18
First day of next month: 2024-02-01

In this example, LocalDate.now() is used to get the current date. Then .with(TemporalAdjusters.firstDayOfNextMonth()) is used to adjust the date to the first day of the next month.

How do I use TemporalAdjusters firstDayOfMonth() method?

The TemporalAdjusters.firstDayOfMonth() method in Java is used to obtain a copy of the current date with the day set to the first day of the current month.

This method is quite simple to use. You need to import the java.time package and use the with() function in conjunction with TemporalAdjusters.firstDayOfMonth().

Here’s a simple example in Java:

package org.kodejava.datetime;

import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;

public class FirstDayOfMonthExample {
    public static void main(String[] args) {
        // Get the current date
        LocalDate date = LocalDate.now();

        // Adjust to first day of current month
        LocalDate firstDayOfMonth = date.with(TemporalAdjusters.firstDayOfMonth());

        System.out.println("Current date: " + date);
        System.out.println("First day of this month: " + firstDayOfMonth);
    }
}

Output:

Current date: 2024-01-18
First day of this month: 2024-01-01

In this example, LocalDate.now() is used to get the current date. Then .with(TemporalAdjusters.firstDayOfMonth()) is used to adjust the date to the first day of the current month.

How do I use TemporalAdjusters dayOfWeekInMonth() method?

dayOfWeekInMonth() is a handy method in java.time.temporal.TemporalAdjusters class that returns an adjuster which changes the date to the n-th day of week in the current month.

Here is an example of how you could use it:

package org.kodejava.datetime;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;

public class DayOfWeekInMonthExample {
    public static void main(String[] args) {
        // Current date
        LocalDate date = LocalDate.now();

        // The 2nd Tuesday in the month of the date.
        LocalDate secondTuesday = date.with(
                TemporalAdjusters.dayOfWeekInMonth(2, DayOfWeek.TUESDAY));

        System.out.println("Current Date: " + date);
        System.out.println("Second Tuesday: " + secondTuesday);
    }
}

Output:

Current Date: 2024-01-18
Second Tuesday: 2024-01-09

This code would display the date of the second Tuesday in the current month.

Here’s what’s going on in this case:

  • TemporalAdjusters.dayOfWeekInMonth(2, DayOfWeek.TUESDAY) specifies that we want the second occurrence of DayOfWeek.TUESDAY in the current month.

  • date.with(TemporalAdjuster) modifies the LocalDate date based on the TemporalAdjuster that is passed in. The original date object is unchanged; a new LocalDate reflecting the adjusted date is returned.

This TemporalAdjusters method is very useful when you have conditional logic based on things like “if it’s the third Monday of the month, then…”