Close

Java Date Time - LocalDateTime.adjustInto() Examples

Java Date Time Java Java API 


Class:

java.time.LocalDateTime

java.lang.Objectjava.lang.Objectjava.time.LocalDateTimejava.time.LocalDateTimejava.time.temporal.TemporalTemporaljava.time.temporal.TemporalAdjusterTemporalAdjusterjava.time.chrono.ChronoLocalDateTimeChronoLocalDateTimejava.io.SerializableSerializableLogicBig

Method:

public Temporal adjustInto(Temporal temporal)

This method adjusts the specified temporal object to have the same date and time fields as this object

This method will throw UnsupportedTemporalTypeException if the provided temporal object does not support the LocalDateTime fields.



Examples


package com.logicbig.example.localdatetime;

import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.Temporal;

public class AdjustIntoExample {

public static void main (String... args) {
LocalDateTime d = LocalDateTime.of(2012, Month.APRIL, 10, 14, 30);
ZonedDateTime z = ZonedDateTime.of(LocalDateTime.now(),
ZoneId.of("America/New_York"));

Temporal t = d.adjustInto(z);
System.out.println(t);

//or alternatively
ZonedDateTime z2 = z.with(d);
System.out.println(z2);
}
}

Output

2012-04-10T14:30-04:00[America/New_York]
2012-04-10T14:30-04:00[America/New_York]




package com.logicbig.example.localdatetime;

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

public class AdjustIntoExample2 {

public static void main (String... args) {
LocalDateTime d = LocalDateTime.of(2012, Month.APRIL, 10, 14, 30);
ZonedDateTime z = ZonedDateTime.now();
Temporal t = d.adjustInto(z);
System.out.println(t);
}
}

Output

2012-04-10T14:30-05:00[America/Chicago]




package com.logicbig.example.localdatetime;

import java.time.LocalDateTime;
import java.time.Month;
import java.time.YearMonth;
import java.time.ZonedDateTime;
import java.time.temporal.Temporal;

public class AdjustIntoExample3 {

public static void main (String... args) {
LocalDateTime d = LocalDateTime.of(2012, Month.APRIL, 10, 14, 30);
YearMonth y = YearMonth.of(2015, Month.AUGUST);
Temporal t = d.adjustInto(y);
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.ChronoLocalDateTime.adjustInto(ChronoLocalDateTime.java:379)
at java.time.LocalDateTime.adjustInto(LocalDateTime.java:1629)
at com.logicbig.example.localdatetime.AdjustIntoExample3.main(AdjustIntoExample3.java:19)
... 6 more




See Also