LocalDateTime withDayOfMonth(int dayOfMonth) example

Description

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;
/*  w w  w  . j a v  a  2s. 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;
//from   w ww. j  a  v  a  2 s.co  m
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.





















Home »
  Java Date Time »
    java.time Reference »




Clock
DayOfWeek
Duration
Instant
LocalDate
LocalDateTime
LocalTime
Month
MonthDay
OffsetDateTime
OffsetTime
Period
Year
YearMonth
ZonedDateTime
ZoneId
ZoneOffset