Thursday, July 4, 2024
HomeLanguagesJavaLocalDateTime withDayOfMonth() method in Java with Examples

LocalDateTime withDayOfMonth() method in Java with Examples

The withDayOfMonth() method of LocalDateTime class in Java is used to get a copy of this LocalDateTime with the dayOfMonth changed to the dayOfMonth passed as the parameter to this method. The remaining values of this LocalDateTime remains the same.

Syntax:

public LocalDateTime withDayOfMonth(int dayOfMonth)

Parameter: This method accepts a single mandatory parameter dayOfMonth which specifies the dayOfMonth to be set in the resultant LocalDateTime instance. The value of this dayOfMonth can range from 1 to 31.

Returns: The function returns a LocalDateTime instance with the dayOfMonth changed to the dayOfMonth passed as the parameter to this method. The remaining values of this LocalDateTime remains the same.

Exceptions: The function throws a DateTimeException if the dayOfMonth value is invalid.

Below programs illustrate the LocalDateTime.withDayOfMonth() method:

Program 1:




// Program to illustrate the withDayOfMonth() method
  
import java.util.*;
import java.time.*;
  
public class GfG {
    public static void main(String[] args)
    {
        // Get the LocalDateTime instance
        LocalDateTime dt = LocalDateTime.now();
  
        // Get the String representation of this LocalDateTime
        System.out.println("Original LocalDateTime: "
                           + dt.toString());
  
        // Get a new LocalDateTime with dayOfMonth 25
        System.out.println("New LocalDateTime: "
                           + dt.withDayOfMonth(25));
    }
}


Output:

Original LocalDateTime: 2018-11-30T10:38:27.429
New LocalDateTime: 2018-11-25T10:38:27.429

Program 2:




// Program to illustrate the withDayOfMonth() method
  
import java.util.*;
import java.time.*;
  
public class GfG {
    public static void main(String[] args)
    {
        // Get the LocalDateTime instance
        LocalDateTime dt
            = LocalDateTime
                  .parse("2015-04-06T10:15:30");
  
        // Get the String representation of this LocalDateTime
        System.out.println("Original LocalDateTime: "
                           + dt.toString());
  
        // Get a new LocalDateTime with dayOfMonth 1
        System.out.println("New LocalDateTime: "
                           + dt.withDayOfMonth(1));
    }
}


Output:

Original LocalDateTime: 2015-04-06T10:15:30
New LocalDateTime: 2015-04-01T10:15:30

Reference: https://docs.oracle.com/javase/10/docs/api/java/time/LocalDateTime.html#withDayOfMonth(int)

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments