Close

Java Date Time - LocalDate.adjustInto() Examples

Java Date Time Java Java API 


Class:

java.time.LocalDate

java.lang.Objectjava.lang.Objectjava.time.LocalDatejava.time.LocalDatejava.time.temporal.TemporalTemporaljava.time.temporal.TemporalAdjusterTemporalAdjusterjava.time.chrono.ChronoLocalDateChronoLocalDatejava.io.SerializableSerializableLogicBig

Method:

Temporal adjustInto(Temporal temporal)

This method adjusts the provided temporal object to this local date object.

It returns the same type as of provided temporal type.

This method replaces the overlapping parts between this LocalDate and the provided temporal object. Returned temporal object has the same fields as of this fields.

This method call will throw UnsupportedTemporalTypeException if the provided temporal object is not adjustable to local date e.g. YearMonth.


Examples:


package com.logicbig.example.localdate;

import java.time.*;
import java.time.temporal.Temporal;

public class AdjustIntoExample {
public static void main (String[] args) {
LocalDate d = LocalDate.of(2010, Month.JANUARY, 10);
Temporal t = d.adjustInto(ZonedDateTime.of(LocalDateTime.now(),
ZoneId.of("America/Chicago")));
System.out.println(t);

}
}

Output

2010-01-10T16:37:27.200-06:00[America/Chicago]




package com.logicbig.example.localdate;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.temporal.Temporal;

public class AdjustIntoExample2 {
public static void main (String[] args) {
LocalDate d = LocalDate.of(2010, Month.JANUARY, 10);
LocalDateTime dt = LocalDateTime.of(2016, Month.APRIL, 20, 12, 30);
Temporal t = d.adjustInto(dt);
System.out.println(t);

}
}

Output

2010-01-10T12:30




package com.logicbig.example.localdate;

import java.time.LocalDate;
import java.time.Month;
import java.time.YearMonth;
import java.time.temporal.Temporal;

public class AdjustIntoExample3 {
public static void main (String[] args) {
LocalDate d = LocalDate.of(2010, Month.JANUARY, 10);
Temporal t = d.adjustInto(YearMonth.of(2015, Month.AUGUST));
System.out.println(t);
}
}

Output

Caused by: java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: EpochDay
at java.time.YearMonth.with(YearMonth.java:693)
at java.time.YearMonth.with(YearMonth.java:131)
at java.time.chrono.ChronoLocalDate.adjustInto(ChronoLocalDate.java:548)
at java.time.LocalDate.adjustInto(LocalDate.java:1550)
at com.logicbig.example.localdate.AdjustIntoExample3.main(AdjustIntoExample3.java:15)
... 6 more




See Also