Friday, June 12, 2026
HomeLanguagesJavaLocalTime withMinute() method in Java with Examples

LocalTime withMinute() method in Java with Examples

The withMinute() method of a LocalTime class is used to get a copy of this LocalTime with the minutes changed to the minutes passed as the parameter to this method. The remaining values of this LocalTime will remain the same. This instance is immutable and unaffected by this method call.

Syntax:

public LocalTime withMinute(int minute)

Parameters: This method accepts a single parameter minute which represents the minute-of-hour to set in the result, from 0 to 59.

Return value: This method returns a LocalTime instance based on this time with the requested minute.

Exception: This method throws a exception DateTimeException if the minute value is invalid

Below programs illustrate the withMinute() method:

Program 1:




// Java program to demonstrate
// LocalTime.withMinute() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a LocalTime object
        LocalTime time
            = LocalTime.parse("19:34:50.63");
  
        // print time
        System.out.println("Old LocalTime: "
                           + time);
  
        // Get a new LocalDateTime with minutes 4
        LocalTime newtime = time.withMinute(4);
  
        // print result
        System.out.println("New LocalDateTime: "
                           + newtime);
    }
}


Output:

Old LocalTime: 19:34:50.630
New LocalDateTime: 19:04:50.630

Program 2:




// Java program to demonstrate
// LocalTime.withMinute() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a LocalTime object
        LocalTime time
            = LocalTime.parse("01:21:30.13");
  
        // print time
        System.out.println("Old LocalTime: "
                           + time);
  
        // Get a new LocalDateTime with minutes 23
        LocalTime newtime = time.withMinute(23);
  
        // print result
        System.out.println("New LocalDateTime: "
                           + newtime);
    }
}


Output:

Old LocalTime: 01:21:30.130
New LocalDateTime: 01:23:30.130

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

RELATED ARTICLES

5 COMMENTS

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12013 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS