Java Date Time - LocalDateTime withDayOfMonth(int dayOfMonth) example








LocalDateTime withDayOfMonth(int dayOfMonth) returns a copy of this LocalDateTime with the day-of-month altered.

Syntax

withDayOfMonth has the following syntax.

public LocalDateTime withDayOfMonth(int dayOfMonth)

Example

The following example shows how to use withDayOfMonth.

import java.time.LocalDateTime;
//from  w  w w.j a v a 2 s  .co m
public class Main {
  public static void main(String[] args) {
    LocalDateTime a = LocalDateTime.of(2014, 6, 30, 12, 01);
    
    LocalDateTime t = a.withDayOfMonth(20);
    
    System.out.println(t);
  }
}

The code above generates the following result.





Example 2

Set day of month and year on Local date time

import java.time.LocalDateTime;
/*w  ww  .  ja  va  2s. com*/
public class Main {
  public static void main(String[] args) {
    
    LocalDateTime timePoint = LocalDateTime.now();
    LocalDateTime thePast = timePoint.withDayOfMonth(10).withYear(2010);
    
    System.out.println(thePast);
  }
}

The code above generates the following result.