Duration minusMinutes(long) method in Java with ExamplesThe minusMinutes(long minutes) method in the Duration class in Java is utilized to subtract the required number of minutes from a Duration instance. The Duration class is one of the classes in the java. It is a time-based feature that was added in Java 8 and represents an amount of time, for example, 34.5 seconds. Method SignatureDuration: The return type of the method is Duration and that tell us that the method returns an object of the Duration class. The returned Duration is equal to the original duration reduced by the given number of minutes. minusMinutes: It is the name of the method. The name of the method itself states that the method is designed to sub several minutes from a duration. long minutes: The method takes a single parameter, minutes, which is of type long. The parameter specifies the number of minutes to subtract from the Duration instance on which the method is called. Example 1: Basic UsageIn this example, we create a Duration object representing a duration of 2 hours using the ofHours method. This duration is stored in the variable duration. We invoke the minusMinutes(30) method on the duration object. This subtracts 30 minutes from the original duration and returns a new Duration object, which we store in the variable resultDuration. We print the original duration and the duration after subtracting 30 minutes. Filename: DurationExample.java Output Original Duration: PT2H Duration after subtracting 30 minutes: PT1H30M Example 2: Handling Negative ResultIn this example, we create a Duration object representing a duration of 45 minutes using the ofMinutes method. We attempt to subtract 60 minutes from the original duration using the minusMinutes method. Since the subtraction exceeds the original duration, the resulting duration is negative (PT-15M). This indicates a duration of 15 minutes in the past. We print both the original duration (PT45M) and the resulting duration after the subtraction (PT-15M) as output. Filename: DurationExample1.java Output Example 1 - Original Duration: PT45M Example 1 - Duration after subtracting 60 minutes: PT-15M Example 2 - Original Duration: PT1H Example 2 - Duration after subtracting 2 hours: PT-1H Example 3: Chaining with Other MethodsIn this example, we create a Duration object representing a duration of 1 hour and 20 minutes using the ofHours and plusMinutes methods. We chain the minusMinutes(50) method to subtract 50 minutes from the original duration. Then, we chain the minusSeconds(10) method to further subtract 10 seconds from the previously obtained duration. The final resulting duration represents 30 minutes and 10 seconds. We print both the original duration and the resulting duration as output. Filename: DurationExample2.java Output Example 1 - Original Duration: PT1H20M Example 1 - Duration after subtracting 50 minutes and 10 seconds: PT29M50S Example 2 - Original Duration: PT1H Example 2 - Duration after subtracting 2 hours and 30 minutes: PT-1H-30M Example 3 - Original Duration: PT5H Example 3 - Duration after subtracting 30000 minutes and 1000 seconds: PT-495H-16M-40S Example 4: Large Number of Minutes to SubtractIn this example, we create a Duration object representing 5 hours. The method ofHours(5) initializes this duration and stores it in the variable duration. We subtract 30,000 minutes from the original duration using minusMinutes(30000). Since 30,000 minutes is equivalent to 500 hours (30,000 / 60), this subtraction results in a significant reduction, turning the duration negative. We then subtract 1000 seconds from the already negative duration using minusSeconds(1000). Filename: LargeNumberOfMinutesExample.java Output Example 1 - Original Duration: PT5H Example 1 - Duration after subtracting 30,000 minutes and 1000 seconds: PT-495H-16M-40S Example 2 - Original Duration: PT48H Example 2 - Duration after subtracting 4320 minutes: PT-24H Example 3 - Original Duration: PT1H Example 3 - Duration after subtracting 5000 minutes and 2000 seconds: PT-82H-53M-20S Applications1. Scheduling Systems Meeting Schedulers: Adjust the duration of meetings or events by subtracting preparation or buffer times. Task Management: Recalculate remaining time for tasks when the start time is delayed or when breaks are taken. 2. Event Countdown Timers Countdown Clocks: Subtract elapsed time from the total duration to display the remaining time until an event. Timers for Sports: Adjust game or match timers dynamically based on pauses or interruptions. 3. Time Tracking Applications Work Loggers: Deduct break times from the total working hours to compute effective working time. Project Management: Track and adjust the remaining time for project tasks when delays occur. |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India