Java Date Time - LocalDateTime with(TemporalField field, long newValue) example








LocalDateTime with(TemporalField field, long newValue) returns a copy of this date-time with the specified field set to a new value.

Syntax

with has the following syntax.

public LocalDateTime with(TemporalField field,   long newValue)

Example

The following example shows how to use with.

import java.time.LocalDateTime;
import java.time.temporal.ChronoField;
// ww w.j ava  2  s.c  o  m
public class Main {
  public static void main(String[] args) {
    LocalDateTime a = LocalDateTime.of(2014, 6, 30, 12, 01);
    
    LocalDateTime t = a.with(ChronoField.YEAR,2012);
    
    System.out.println(t);
  }
}

The code above generates the following result.