LocalDateTime withMinute() method in Java with Examples

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

Syntax:

public LocalDateTime withMinute(int minutes)

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



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

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

Below programs illustrate the LocalDateTime.withMinute() method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to illustrate the withMinute() 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 minutes 0
        System.out.println("New LocalDateTime: "
                           + dt.withMinute(0));
    }
}

chevron_right


Output:

Original LocalDateTime: 2018-11-30T12:52:26.105
New LocalDateTime: 2018-11-30T12:00:26.105

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to illustrate the withMinute() 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 minutes 59
        System.out.println("New LocalDateTime: "
                           + dt.withMinute(59));
    }
}

chevron_right


Output:

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

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



My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.