Core Java

Java 8 TemporalAdjusters Example

1. Introduction

Java 8 Date-Time API is a JSR-310 implementation. It has a new set of packages to provide a comprehensive date-time model.

Java 8 Temporal is an interface which defines read-write access to a temporal object, such as the date, time, offset or a combination of these. Java 8 provides many common implementation classes: LocalDate, LocalTime, LocalDateTime, ZonedDateTime, Instant, JapaneseDate, ThaiBuddhistDate, HijrahDate, MinguoDate, etc.

 
Java 8 TemporalAdjusters is a class which contains predefined static implementations for many common useful methods to modify temporal objects.

Method NameDescription
firstDayOfMonthReturns the “first day of month” adjuster, which returns a new date set it to the first day of the current month.
lastDayOfMonthReturns the “last day of month” adjuster, which returns a new date set it to the last day of the current month.
firstDayOfNextMonthReturns the “first day of next month” adjuster, which returns a new date set it to the first day of the next month.
firstDayOfYearReturns the “first day of the year” adjuster, which returns a new date set it to the first day of the current year.
lastDayOfYearReturns the “last day of the year” adjuster, which returns a new date set it to the last day of the current year.
firstDayOfNextYearReturns the “first day of next year” adjuster, which returns a new date set it to the first day of the next year.
firstInMonthReturns the “first day-of-week within a month” adjuster, which returns a new date in the same month with the first matching day-of-week. Such as “first Wednesday in June”.
lastInMonthReturns the “last day-of-week within a month adjuster”, such as “last Thursday in November”
dayOfWeekInMonthReturns the “day-of-week in month” adjuster, which returns a new date in the same month with the ordinal day-of-week.
nextReturns the “next day-of-week” adjuster, which adjusts the date to the first occurrence of the specified day-of-week after the date being adjusted.
previousReturns the “previous day-of-week” adjuster, which adjusts the date to the first occurrence of the specified day-of-week before the date being adjusted.

In this example, I will demonstrate how to adjust the date in LocalDate, LocalDateTime, ZonedDateTime, JapaneseDateThaiBuddhistDate, HijrahDate, MinguoDate using TemporalAdjusters.

2. Technologies used

The example code in this article was built and run using:

  • Java 1.8.101
  • Maven 3.3.9
  • Eclipse Oxygen
  • JUnit 4.12

3. Maven Project

3.1 Dependency

Add JUnit to the pom.xml.

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>zheng.jcg.demo</groupId>
	<artifactId>java8-demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.3</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

4. Custom TemporalAdjuster

4.1 HolidayAdjuster

If you work on a system which needs to find holidays like Mother’s Day, Father’s Day, Labor Day, or Thanksgiving, you can do so with TemporalAdjusters.dayOfWeekInMonth in a few lines of codes. In this step, I will create a HolidayAdjuster class to find a holiday as a LocalDate.

HolidayAdjuster.java

package com.zheng.demo;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;

public class HolidayAdjuster implements TemporalAdjuster {

	private static final int FIRST_DAY = 1;

	public HolidayAdjuster(Month month, int ordinal, DayOfWeek dayOfWeek) {
		super();
		this.month = month;
		this.dayOfWeek = dayOfWeek;
		this.ordinal = ordinal;
	}

	private Month month;
	private DayOfWeek dayOfWeek;
	private int ordinal;

	@Override
	public Temporal adjustInto(Temporal temporalAdjusterInput) {
		LocalDate temporalAdjusterDate = LocalDate.from(temporalAdjusterInput);
		LocalDate holidayMonth = LocalDate.of(temporalAdjusterDate.getYear(), month, FIRST_DAY);
		return holidayMonth.with(TemporalAdjusters.dayOfWeekInMonth(ordinal, dayOfWeek));		
	}

}

4.2 HolidayAdjusterTest

In this step, I will create four unit test cases to output the date for Mother’s day, Father’s Day, Labor Day, and Thanksgiving. Please note that the Temporal must be a LocalDate type.

HolidayAdjusterTest.java

package com.zheng.demo;

import java.time.DateTimeException;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Month;

import org.junit.Before;
import org.junit.Test;

public class HolidayAdjusterTest {

	private LocalDate localDate;

	@Before
	public void setup() {
		localDate = LocalDate.now();
		System.out.print("\nCurrent localDate : " + localDate + ". ");
	}

	@Test
	public void mothersDay_customerAdjuster() {
		HolidayAdjuster mothersDayAdjuster = new HolidayAdjuster(Month.MAY, 2, DayOfWeek.SUNDAY);
		LocalDate mothersDay = localDate.with(mothersDayAdjuster);
		System.out.println("Mother's Day : " + mothersDay);
	}

	@Test
	public void fathersDay_customerAdjuster() {
		HolidayAdjuster fathersDayAdjuster = new HolidayAdjuster(Month.JUNE, 3, DayOfWeek.SUNDAY);
		LocalDate fathersDay = localDate.with(fathersDayAdjuster);
		System.out.println("Father's Day : " + fathersDay);
	}

	@Test
	public void laborDay_customerAdjuster() {
		HolidayAdjuster laborDayAdjuster = new HolidayAdjuster(Month.SEPTEMBER, 1, DayOfWeek.MONDAY);
		LocalDate laborDay = localDate.with(laborDayAdjuster);
		System.out.println("Labor Day : " + laborDay);
	}

	@Test
	public void thanksGivingDate_customAdjuster() {
		HolidayAdjuster thanksgivingAdjuster = new HolidayAdjuster(Month.NOVEMBER, 4, DayOfWeek.THURSDAY);
		LocalDate thanksGivingDay = localDate.with(thanksgivingAdjuster);
		System.out.println("Thanksgiving Date: " + thanksGivingDay);
	}

	@Test(expected=DateTimeException.class)
	public void exception_wrongtype() {
		LocalTime test = LocalTime.now();
		System.out.println("Throw Exception if not LocalDate.");
		test.with(new HolidayAdjuster(Month.NOVEMBER, 4,
				DayOfWeek.THURSDAY));
	}

}

Execute HolidayAdjusterTest as Junit test and capture the output here.

Junit Output

Current localDate : 2018-11-28. Labor Day : 2018-09-03

Current localDate : 2018-11-28. Father's Day : 2018-06-17

Current localDate : 2018-11-28. Thanksgiving Date: 2018-11-22

Current localDate : 2018-11-28. Throw Exception if not LocalDate.

Current localDate : 2018-11-28. Mother's Day : 2018-05-13

4.3 PaydayAdjuster

Company A schedules to cut the paycheck at 10 AM twice a month, one is on the 15th day of every month, the other is on the last day of every month. If it is on a Saturday or Sunday, then it is set to Friday. In this step, I will create a PayDayAdjuster class to find the next payday as a LocalDateTime with the previous and lastDayOfMonth methods.

PaydayAdjuster.java

package com.zheng.demo;

import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;

public class PaydayAdjuster implements TemporalAdjuster {

	private static final int TEN_OCLOCK = 10;
	private static final int MID_MONTH_DAY = 15;

	public PaydayAdjuster() {
		super();

	}

	@Override
	public Temporal adjustInto(Temporal temporalAdjusterInput) {
		LocalDateTime orgDate = LocalDateTime.from(temporalAdjusterInput);
		LocalDateTime date = LocalDateTime.of(orgDate.getYear(), orgDate.getMonth(), orgDate.getDayOfMonth(),
				TEN_OCLOCK, 0);

		int day;
		if (date.getDayOfMonth() < MID_MONTH_DAY) {
			day = MID_MONTH_DAY;
		} else {
			day = date.with(TemporalAdjusters.lastDayOfMonth()).getDayOfMonth();
		}
		date = date.withDayOfMonth(day);
		if (date.getDayOfWeek() == DayOfWeek.SATURDAY || date.getDayOfWeek() == DayOfWeek.SUNDAY) {
			date = date.with(TemporalAdjusters.previous(DayOfWeek.FRIDAY));
		}

		return temporalAdjusterInput.with(date);
	}

}

4.4 PaydayAdjusterTest

In this step, I will create unit test cases to output the next payday. Please note that Temporal must be a LocalDateTime type.

PaydayAdjusterTest.java

package com.zheng.demo;

import java.time.DateTimeException;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.temporal.TemporalAdjusters;

import org.junit.Before;
import org.junit.Test;

public class PaydayAdjusterTest {

	private LocalDateTime localDate;

	@Before
	public void setup() {
		localDate = LocalDateTime.now();
		System.out.print("\nCurrent LocalDateTime : " + localDate + ". ");
	}

	@Test
	public void nextPayDay() {
		LocalDateTime payDate = localDate.with(new PaydayAdjuster());
		System.out.println("Next Pay Day : " + payDate);
	}

	@Test
	public void nextMonthPayDay() {
		LocalDateTime firstMonth = localDate.with(TemporalAdjusters.firstDayOfNextMonth());
		LocalDateTime payDate = firstMonth.with(new PaydayAdjuster());
		System.out.println("Next Month Pay Day : " + payDate);
	}

	@Test(expected = DateTimeException.class)
	public void exception_wrongtype() {
		LocalDate test = LocalDate.now();
		System.out.println("Throw Exception if not LocalDateTime.");
		test.with(new HolidayAdjuster(Month.NOVEMBER, 4, DayOfWeek.THURSDAY));
	}

}

Execute it as Junit test and capture the output.

PaydayAdjusterTest Output

Current LocalDateTime : 2018-11-28T08:55:55.887. Next Month Pay Day : 2018-12-14T10:00

Current LocalDateTime : 2018-11-28T08:55:55.950. Next Pay Day : 2018-11-30T10:00

Current LocalDateTime : 2018-11-28T08:55:55.950. Throw Exception if not LocalDateTime.

5. JUnit Tests

Java 8 TemporalAdjusters provides lots of out-of-box solutions for adjusting the date. In this step, I will create JUnit tests to demonstrate how to use these pre-defined static methods to modify temporal objects.

5.1 TemporalAdjusters_LocalDateTest

Java 8 LocalDate represents a date without a time zone or time. In this step, I will adjust the year, month, and day with the pre-defined methods: firstDayOfYear, firstDayOfMonth, etc.

TemporalAdjusters_LocalDateTest.java

package com.zheng.demo;

import static org.junit.Assert.assertEquals;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;

import org.junit.Before;
import org.junit.Test;

public class TemporalAdjusters_LocalDateTest {

	private LocalDate localDate;

	@Before
	public void setup() {
		localDate = LocalDate.now();
		System.out.println("\nCurrent localDate : " + localDate);
	}

	@Test
	public void adjuster_firstDayOfMonth() {
		LocalDate updatedDate = localDate.with(TemporalAdjusters.firstDayOfMonth());
		System.out.println("firstDayOfMonth : " + updatedDate);
	}

	@Test
	public void adjuster_firstDayOfYear() {
		LocalDate updatedDate = localDate.with(TemporalAdjusters.firstDayOfYear());
		System.out.println("firstDayOfYear : " + updatedDate);
	}

	@Test
	public void adjuster_lastDayOfYear() {
		LocalDate updatedDate = localDate.with(TemporalAdjusters.lastDayOfYear());
		System.out.println("lastDayOfYear : " + updatedDate);
	}

	@Test
	public void adjuster_firstDayOfNextYear() {
		LocalDate updatedDate = localDate.with(TemporalAdjusters.firstDayOfNextYear());
		System.out.println("firstDayOfNextYear : " + updatedDate);
	}

	@Test
	public void adjuster_firstDayOfNextMonth() {
		LocalDate updatedDate = localDate.with(TemporalAdjusters.firstDayOfNextMonth());
		System.out.println("firstDayOfNextMonth : " + updatedDate);
	}

	@Test
	public void adjuster_lastDayOfMonth() {
		LocalDate updatedDate = localDate.with(TemporalAdjusters.lastDayOfMonth());
		System.out.println("lastDayOfMonth : " + updatedDate);

	}

	@Test
	public void adjuster_nextMonday() {
		LocalDate updatedDate = localDate.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
		System.out.println("nextMonday : " + updatedDate);
	}

	@Test
	public void adjuster_nextSunday() {
		LocalDate localDate = LocalDate.of(2018, 11, 10);
		LocalDate nextSunday = localDate.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));

		assertEquals("2018-11-11", nextSunday.toString());
	}

	@Test
	public void adjuster_firstInMonth() {
		LocalDate updatedDate = localDate.with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
		System.out.println("firstInMonth_Monday: " + updatedDate);
	}

	@Test
	public void adjuster_previousWednesday() {
		LocalDate updatedDate = localDate.with(TemporalAdjusters.previous(DayOfWeek.WEDNESDAY));
		System.out.println("previousWednesday : " + updatedDate);
	}

	@Test
	public void christmasDay_customAdjuster() {
		TemporalAdjuster xmas = temporal -> temporal.with(ChronoField.MONTH_OF_YEAR, 12).with(ChronoField.DAY_OF_MONTH,
				25);

		LocalDate xmasDate = localDate.with(xmas);
		System.out.println("Christmas : " + xmasDate + ", it's on " + xmasDate.getDayOfWeek());
	}

	@Test
	public void tenDaysLater() {
		TemporalAdjuster tenDaysFromNow = temporal -> temporal.plus(10, ChronoUnit.DAYS);

		LocalDate localTimeUpdate = localDate.with(tenDaysFromNow);
		System.out.println("Ten Days later : " + localTimeUpdate);
	}

}

5.2 TemporalAdjusters_LocalDateTimeTest

Java 8 LocalDateTime represents a date without a time zone. I will repeat the same adjustment for the LocalDateTime type.

TemporalAdjusters_LocalDateTimeTest.java

package com.zheng.demo;

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;

import org.junit.Before;
import org.junit.Test;

public class TemporalAdjusters_LocalDateTimeTest {

	private LocalDateTime localDateTime;

	@Before
	public void setup() {
		localDateTime = LocalDateTime.now();
		System.out.println("\nCurrent LocalDateTime : " + localDateTime);
	}

	@Test
	public void adjuster_firstDayOfMonth() {
		LocalDateTime updatedDate = localDateTime.with(TemporalAdjusters.firstDayOfMonth());
		System.out.println("firstDayOfMonth : " + updatedDate);
	}

	@Test
	public void adjuster_firstDayOfNextMonth() {
		LocalDateTime updatedDate = localDateTime.with(TemporalAdjusters.firstDayOfNextMonth());
		System.out.println("firstDayOfNextMonth : " + updatedDate);
	}

	@Test
	public void adjuster_lastDayOfMonth() {
		LocalDateTime updatedDate = localDateTime.with(TemporalAdjusters.lastDayOfMonth());
		System.out.println("lastDayOfMonth : " + updatedDate);

	}

	@Test
	public void adjuster_nextMonday() {
		LocalDateTime updatedDate = localDateTime.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
		System.out.println("Next Monday : " + updatedDate);
	}

	@Test
	public void adjuster_previousWednesday() {
		LocalDateTime updatedDate = localDateTime.with(TemporalAdjusters.previous(DayOfWeek.WEDNESDAY));
		System.out.println("Previous Wednesday : " + updatedDate);
	}

	@Test
	public void christmasDay() {
		TemporalAdjuster xmas = temporal -> temporal.with(ChronoField.MONTH_OF_YEAR, 12).with(ChronoField.DAY_OF_MONTH,
				25);

		LocalDateTime xmasDate = localDateTime.with(xmas);
		System.out.println("Christmas : " + xmasDate + ", it's on " + xmasDate.getDayOfWeek());
	}

	@Test
	public void tenDaysThreeHourLater() {
		TemporalAdjuster tenDaysThreeHoursFromNow = temporal -> temporal.plus(10, ChronoUnit.DAYS)
				.plus(Duration.ofHours(3));

		LocalDateTime localTimeUpdate = localDateTime.with(tenDaysThreeHoursFromNow);
		System.out.println("Ten Days later : " + localTimeUpdate);
	}

}

5.3 TemporalAdjusters_ZonedDateTimeTest

Java 8 ZonedDateTime represents a date with a time zone. In this step, I will repeat the same adjustment for the ZonedDateTime class.

TemporalAdjusters_ZonedDateTimeTest.java

package com.zheng.demo;

import java.time.DayOfWeek;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;

import org.junit.Before;
import org.junit.Test;

public class TemporalAdjusters_ZonedDateTimeTest {

	private ZonedDateTime zonedDateTime;

	@Before
	public void setup() {
		zonedDateTime = ZonedDateTime.now();
		System.out.println("\nCurrent ZonedDateTime : " + zonedDateTime);
	}

	@Test
	public void adjuster_firstDayOfMonth() {
		ZonedDateTime updatedDate = zonedDateTime.with(TemporalAdjusters.firstDayOfMonth());
		System.out.println("firstDayOfMonth : " + updatedDate);
	}

	@Test
	public void adjuster_firstDayOfYear() {
		ZonedDateTime updatedDate = zonedDateTime.with(TemporalAdjusters.firstDayOfYear());
		System.out.println("firstDayOfYear : " + updatedDate);
	}

	@Test
	public void adjuster_lastDayOfYear() {
		ZonedDateTime updatedDate = zonedDateTime.with(TemporalAdjusters.lastDayOfYear());
		System.out.println("lastDayOfYear : " + updatedDate);
	}

	@Test
	public void adjuster_firstDayOfNextYear() {
		ZonedDateTime updatedDate = zonedDateTime.with(TemporalAdjusters.firstDayOfNextYear());
		System.out.println("firstDayOfNextYear : " + updatedDate);
	}

	@Test
	public void adjuster_firstDayOfNextMonth() {
		ZonedDateTime updatedDate = zonedDateTime.with(TemporalAdjusters.firstDayOfNextMonth());
		System.out.println("firstDayOfNextMonth : " + updatedDate);
	}

	@Test
	public void adjuster_lastDayOfMonth() {
		ZonedDateTime updatedDate = zonedDateTime.with(TemporalAdjusters.lastDayOfMonth());
		System.out.println("lastDayOfMonth : " + updatedDate);

	}

	@Test
	public void adjuster_nextMonday() {
		ZonedDateTime updatedDate = zonedDateTime.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
		System.out.println("nextMonday : " + updatedDate);
	}

	@Test
	public void adjuster_firstInMonth() {
		ZonedDateTime updatedDate = zonedDateTime.with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
		System.out.println("firstInMonth_Monday: " + updatedDate);
	}

	@Test
	public void adjuster_previousWednesday() {
		ZonedDateTime updatedDate = zonedDateTime.with(TemporalAdjusters.previous(DayOfWeek.WEDNESDAY));
		System.out.println("previousWednesday : " + updatedDate);
	}

	@Test
	public void christmasDay() {
		TemporalAdjuster xmas = temporal -> temporal.with(ChronoField.MONTH_OF_YEAR, 12).with(ChronoField.DAY_OF_MONTH,
				25);

		ZonedDateTime xmasDate = zonedDateTime.with(xmas);
		System.out.println("Christmas : " + xmasDate + ", it's on " + xmasDate.getDayOfWeek());
	}

	@Test
	public void tenDaysLater() {
		TemporalAdjuster tenDaysFromNow = temporal -> temporal.plus(10, ChronoUnit.DAYS);

		ZonedDateTime localTimeUpdate = zonedDateTime.with(tenDaysFromNow);
		System.out.println("Ten Days later : " + localTimeUpdate);
	}
	
}

5.4 TemporalAdjusters_MinguoDateTest

Java 8 MinGuoDate represents a date in the Minguo calendar system. It is primarily used in the Republic of China, often known as Taiwan. I will repeat the same adjustment for the MinGuoDate class.

TemporalAdjusters_MinguoDateTest.java

package com.zheng.demo;

import java.time.DayOfWeek;
import java.time.chrono.MinguoDate;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;

import org.junit.Before;
import org.junit.Test;

public class TemporalAdjusters_MinguoDateTest {

	private MinguoDate minguoDate;

	@Before
	public void setup() {
		minguoDate = MinguoDate.now();
		System.out.println("\nCurrent MinguoDate : " + minguoDate);
	}

	@Test
	public void adjuster_firstDayOfMonth() {
		MinguoDate updatedDate = minguoDate.with(TemporalAdjusters.firstDayOfMonth());
		System.out.println("firstDayOfMonth : " + updatedDate);
	}

	@Test
	public void adjuster_firstDayOfYear() {
		MinguoDate updatedDate = minguoDate.with(TemporalAdjusters.firstDayOfYear());
		System.out.println("firstDayOfYear : " + updatedDate);
	}

	@Test
	public void adjuster_lastDayOfYear() {
		MinguoDate updatedDate = minguoDate.with(TemporalAdjusters.lastDayOfYear());
		System.out.println("lastDayOfYear : " + updatedDate);
	}

	@Test
	public void adjuster_firstDayOfNextYear() {
		MinguoDate updatedDate = minguoDate.with(TemporalAdjusters.firstDayOfNextYear());
		System.out.println("firstDayOfNextYear : " + updatedDate);
	}

	@Test
	public void adjuster_firstDayOfNextMonth() {
		MinguoDate updatedDate = minguoDate.with(TemporalAdjusters.firstDayOfNextMonth());
		System.out.println("firstDayOfNextMonth : " + updatedDate);
	}

	@Test
	public void adjuster_lastDayOfMonth() {
		MinguoDate updatedDate = minguoDate.with(TemporalAdjusters.lastDayOfMonth());
		System.out.println("lastDayOfMonth : " + updatedDate);

	}

	@Test
	public void adjuster_nextMonday() {
		MinguoDate updatedDate = minguoDate.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
		System.out.println("nextMonday : " + updatedDate);
	}


	@Test
	public void adjuster_firstInMonth() {
		MinguoDate updatedDate = minguoDate.with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
		System.out.println("firstInMonth_Monday: " + updatedDate);
	}

	@Test
	public void adjuster_previousWednesday() {
		MinguoDate updatedDate = minguoDate.with(TemporalAdjusters.previous(DayOfWeek.WEDNESDAY));
		System.out.println("previousWednesday : " + updatedDate);
	}

	@Test
	public void tenDaysLater() {
		TemporalAdjuster tenDaysFromNow = temporal -> temporal.plus(10, ChronoUnit.DAYS);

		MinguoDate localTimeUpdate = minguoDate.with(tenDaysFromNow);
		System.out.println("Ten Days later : " + localTimeUpdate);
	}

}

5.5 TemporalAdjusters_JapaneseDateTest

Java 8 JapaneseDate represents a date in the Japanese Imperial calendar system. I will repeat the same adjustment for the JapaneseDate class.

TemporalAdjusters_JapaneseDateTest.java

package com.zheng.demo;

import java.time.DayOfWeek;
import java.time.chrono.JapaneseDate;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;

import org.junit.Before;
import org.junit.Test;

public class TemporalAdjusters_JapaneseDateTest {

	private JapaneseDate japaneseDate;

	@Before
	public void setup() {
		japaneseDate = JapaneseDate.now();
		System.out.println("\nCurrent JapaneseDate : " + japaneseDate);
	}

	@Test
	public void adjuster_firstDayOfMonth() {
		JapaneseDate updatedDate = japaneseDate.with(TemporalAdjusters.firstDayOfMonth());
		System.out.println("firstDayOfMonth : " + updatedDate);
	}

	@Test
	public void adjuster_firstDayOfYear() {
		JapaneseDate updatedDate = japaneseDate.with(TemporalAdjusters.firstDayOfYear());
		System.out.println("firstDayOfYear : " + updatedDate);
	}

	@Test
	public void adjuster_lastDayOfYear() {
		JapaneseDate updatedDate = japaneseDate.with(TemporalAdjusters.lastDayOfYear());
		System.out.println("lastDayOfYear : " + updatedDate);
	}

	@Test
	public void adjuster_firstDayOfNextYear() {
		JapaneseDate updatedDate = japaneseDate.with(TemporalAdjusters.firstDayOfNextYear());
		System.out.println("firstDayOfNextYear : " + updatedDate);
	}

	@Test
	public void adjuster_firstDayOfNextMonth() {
		JapaneseDate updatedDate = japaneseDate.with(TemporalAdjusters.firstDayOfNextMonth());
		System.out.println("firstDayOfNextMonth : " + updatedDate);
	}

	@Test
	public void adjuster_lastDayOfMonth() {
		JapaneseDate updatedDate = japaneseDate.with(TemporalAdjusters.lastDayOfMonth());
		System.out.println("lastDayOfMonth : " + updatedDate);

	}

	@Test
	public void adjuster_nextMonday() {
		JapaneseDate updatedDate = japaneseDate.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
		System.out.println("nextMonday : " + updatedDate);
	}


	@Test
	public void adjuster_firstInMonth() {
		JapaneseDate updatedDate = japaneseDate.with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
		System.out.println("firstInMonth_Monday: " + updatedDate);
	}

	@Test
	public void adjuster_previousWednesday() {
		JapaneseDate updatedDate = japaneseDate.with(TemporalAdjusters.previous(DayOfWeek.WEDNESDAY));
		System.out.println("previousWednesday : " + updatedDate);
	}

	@Test
	public void tenDaysLater() {
		TemporalAdjuster tenDaysFromNow = temporal -> temporal.plus(10, ChronoUnit.DAYS);

		JapaneseDate localTimeUpdate = japaneseDate.with(tenDaysFromNow);
		System.out.println("Ten Days later : " + localTimeUpdate);
	}

}

6. Summary

In this example, we reviewed Java 8 Date time API and demonstrated how to use the pre-defined static methods in TemporalAdjusters to adjust the date at LocalDate, LocalDateTime, JapaneseDate, MinguoDate, and ZonedDateTime.

7. Download the Source Code

This example consists of a Maven project which adjusts LocalDate, LocatDateTime, JapaneseDate, MinguoDate, and ZonedDateTime using Java 8 TemporalAdjusters.

Download
You can download the full source code of this example here: Java 8 TemporalAdjusters Example

Mary Zheng

Mary has graduated from Mechanical Engineering department at ShangHai JiaoTong University. She also holds a Master degree in Computer Science from Webster University. During her studies she has been involved with a large number of projects ranging from programming and software engineering. She works as a senior Software Engineer in the telecommunications sector where she acts as a leader and works with others to design, implement, and monitor the software solution.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button