Close

Java Date Time - OffsetDateTime.with() Examples

Java Date Time Java Java API 


Class:

java.time.OffsetDateTime

java.lang.Objectjava.lang.Objectjava.time.OffsetDateTimejava.time.OffsetDateTimejava.time.temporal.TemporalTemporaljava.time.temporal.TemporalAdjusterTemporalAdjusterjava.lang.ComparableComparablejava.io.SerializableSerializableLogicBig

Methods:

public OffsetDateTime with(TemporalAdjuster adjuster)

Returns an adjusted copy of this date-time. The adjustment logic is provided by the 'adjuster'.


public OffsetDateTime with(TemporalField field,  long newValue)

Returns a copy of this date-time with the specified field set to a new value.



Examples


package com.logicbig.example.offsetdatetime;

import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.temporal.TemporalAdjusters;
import java.util.function.UnaryOperator;

public class WithExample {

public static void main(String... args) {
OffsetDateTime d = OffsetDateTime.of(2016, 12, 15, 18,
29, 32, 3000000, ZoneOffset.ofHours(-5));
System.out.println(d);

OffsetDateTime d2 = d.with(TemporalAdjusters.firstDayOfMonth());
System.out.println(d2);

OffsetDateTime d3 = d.with(TemporalAdjusters.ofDateAdjuster(localDate -> localDate.plusDays(300)));
System.out.println(d3);
}
}

Output

2016-12-15T18:29:32.003-05:00
2016-12-01T18:29:32.003-05:00
2017-10-11T18:29:32.003-05:00




package com.logicbig.example.offsetdatetime;

import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.temporal.ChronoField;

public class WithExample2 {

public static void main(String... args) {
OffsetDateTime d = OffsetDateTime.of(2016, 12, 15, 18,
29, 32, 3000000, ZoneOffset.ofHours(-5));
System.out.println("Given d = " + d);

for (ChronoField chronoField : ChronoField.values()) {
with(d, chronoField);
}
}

private static void with(OffsetDateTime d, ChronoField chronoField) {
OffsetDateTime d2 = d.with(chronoField, 1);
System.out.printf("With 1st %s of d => %s%n", chronoField.name(), d2);
}
}

Output

Given d = 2016-12-15T18:29:32.003-05:00
With 1st NANO_OF_SECOND of d => 2016-12-15T18:29:32.000000001-05:00
With 1st NANO_OF_DAY of d => 2016-12-15T00:00:00.000000001-05:00
With 1st MICRO_OF_SECOND of d => 2016-12-15T18:29:32.000001-05:00
With 1st MICRO_OF_DAY of d => 2016-12-15T00:00:00.000001-05:00
With 1st MILLI_OF_SECOND of d => 2016-12-15T18:29:32.001-05:00
With 1st MILLI_OF_DAY of d => 2016-12-15T00:00:00.001-05:00
With 1st SECOND_OF_MINUTE of d => 2016-12-15T18:29:01.003-05:00
With 1st SECOND_OF_DAY of d => 2016-12-15T00:00:01.003-05:00
With 1st MINUTE_OF_HOUR of d => 2016-12-15T18:01:32.003-05:00
With 1st MINUTE_OF_DAY of d => 2016-12-15T00:01:32.003-05:00
With 1st HOUR_OF_AMPM of d => 2016-12-15T13:29:32.003-05:00
With 1st CLOCK_HOUR_OF_AMPM of d => 2016-12-15T13:29:32.003-05:00
With 1st HOUR_OF_DAY of d => 2016-12-15T01:29:32.003-05:00
With 1st CLOCK_HOUR_OF_DAY of d => 2016-12-15T01:29:32.003-05:00
With 1st AMPM_OF_DAY of d => 2016-12-15T18:29:32.003-05:00
With 1st DAY_OF_WEEK of d => 2016-12-12T18:29:32.003-05:00
With 1st ALIGNED_DAY_OF_WEEK_IN_MONTH of d => 2016-12-15T18:29:32.003-05:00
With 1st ALIGNED_DAY_OF_WEEK_IN_YEAR of d => 2016-12-09T18:29:32.003-05:00
With 1st DAY_OF_MONTH of d => 2016-12-01T18:29:32.003-05:00
With 1st DAY_OF_YEAR of d => 2016-01-01T18:29:32.003-05:00
With 1st EPOCH_DAY of d => 1970-01-02T18:29:32.003-05:00
With 1st ALIGNED_WEEK_OF_MONTH of d => 2016-12-01T18:29:32.003-05:00
With 1st ALIGNED_WEEK_OF_YEAR of d => 2016-01-07T18:29:32.003-05:00
With 1st MONTH_OF_YEAR of d => 2016-01-15T18:29:32.003-05:00
With 1st PROLEPTIC_MONTH of d => 0000-02-15T18:29:32.003-05:00
With 1st YEAR_OF_ERA of d => 0001-12-15T18:29:32.003-05:00
With 1st YEAR of d => 0001-12-15T18:29:32.003-05:00
With 1st ERA of d => 2016-12-15T18:29:32.003-05:00
With 1st INSTANT_SECONDS of d => 1969-12-31T19:00:01.003-05:00
With 1st OFFSET_SECONDS of d => 2016-12-15T18:29:32.003+00:00:01




See Also