Java LocalTime withMinute() Method

The withMinute() method in Java, part of the java.time.LocalTime class, is used to create a copy of the current LocalTime instance with the specified minute-of-hour value. This method is useful when you need to adjust the minute component of a LocalTime instance while keeping the other components (hour, second, and nanosecond) unchanged.

Table of Contents

  1. Introduction
  2. withMinute() Method Syntax
  3. Understanding withMinute()
  4. Examples
    • Basic Usage
    • Using withMinute() in Conditional Statements
  5. Real-World Use Case
  6. Conclusion

Introduction

The withMinute() method allows you to create a new LocalTime instance with a specified minute-of-hour value. This is particularly useful when you need to adjust the minute component of a time while keeping the hour, second, and nanosecond components unchanged.

withMinute() Method Syntax

The syntax for the withMinute() method is as follows:

public LocalTime withMinute(int minute)

Parameters:

  • minute: The minute-of-hour to set in the resulting LocalTime, from 0 to 59.

Returns:

  • A LocalTime based on the current time with the requested minute, not null.

Throws:

  • DateTimeException if the minute value is invalid.

Understanding withMinute()

The withMinute() method returns a copy of the current LocalTime instance with the specified minute value. The hour, second, and nanosecond values remain unchanged. This method is immutable and does not modify the original LocalTime instance.

Examples

Basic Usage

To demonstrate the basic usage of withMinute(), we will create a new LocalTime instance with a specified minute value.

Example

import java.time.LocalTime;

public class LocalTimeWithMinuteExample {
    public static void main(String[] args) {
        LocalTime time = LocalTime.of(10, 30, 45); // 10:30:45 AM
        LocalTime newTime = time.withMinute(15); // Set minute to 15

        System.out.println("Original Time: " + time);
        System.out.println("New Time: " + newTime);
    }
}

Output:

Original Time: 10:30:45
New Time: 10:15:45

Using withMinute() in Conditional Statements

This example shows how to use the withMinute() method in conditional statements to perform actions based on the adjusted time.

Example

import java.time.LocalTime;

public class LocalTimeConditionalExample {
    public static void main(String[] args) {
        LocalTime currentTime = LocalTime.now();
        LocalTime adjustedTime = currentTime.withMinute(0); // Set minute to 0

        if (adjustedTime.isBefore(currentTime)) {
            System.out.println("The adjusted time is before the current time.");
        } else {
            System.out.println("The adjusted time is after or equal to the current time.");
        }
    }
}

Output:

The adjusted time is before the current time.

Real-World Use Case

Setting Specific Minutes for Events

In real-world applications, the withMinute() method can be used to set specific minutes for events or tasks while keeping the other components of the time unchanged.

Example

import java.time.LocalTime;

public class EventSchedulerExample {
    public static void main(String[] args) {
        LocalTime eventTime = LocalTime.of(14, 30); // 2:30 PM
        LocalTime newEventTime = eventTime.withMinute(45); // Change event time to 2:45 PM

        System.out.println("Original Event Time: " + eventTime);
        System.out.println("New Event Time: " + newEventTime);
    }
}

Output:

Original Event Time: 14:30
New Event Time: 14:45

Conclusion

The LocalTime.withMinute() method is used to create a copy of the current LocalTime instance with a specified minute value. This method is particularly useful for adjusting the minute component of a time while keeping the other components unchanged. By understanding and using the withMinute() method, you can effectively manage and manipulate time-based data in your Java applications.

Comments