🎓 Check Out My Top 25 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare
In this guide, you will learn about the Duration toMinutes() method in Java programming and how to use it with an example.
1. Duration toMinutes() Method Overview
Definition:
The Duration.toMinutes() method is employed to obtain the total number of minutes represented by this duration, including both the whole hours and minutes.
Syntax:
public long toMinutes()
Parameters:
- None.
Key Points:
- The method returns the total count of minutes in the Duration.
- This includes both the whole hours (converted to minutes) and any additional minutes.
- A negative Duration will result in a negative returned value.
- This method can be useful for operations that require durations in minute granularity or when displaying the duration in minutes.
2. Duration toMinutes() Method Example
import java.time.Duration;
public class DurationToMinutesExample {
public static void main(String[] args) {
Duration duration1 = Duration.ofHours(2).plusMinutes(30); // 2 hours and 30 minutes
Duration duration2 = Duration.ofHours(-1).minusMinutes(15); // -1 hour and 15 minutes
// Convert durations to minutes
long minutes1 = duration1.toMinutes();
long minutes2 = duration2.toMinutes();
// Print the minutes
System.out.println("Duration1 in minutes: " + minutes1);
System.out.println("Duration2 in minutes: " + minutes2);
}
}
Output:
Duration1 in minutes: 150 Duration2 in minutes: -75
Explanation:
In the example, duration1 is constructed with a total of 2 hours and 30 minutes. When toMinutes() is called, it returns 150 which is the total minutes (120 minutes from the 2 hours and 30 additional minutes).
For duration2, which is -1 hour and 15 minutes, the toMinutes() method gives -75 as the result. This is because 1 hour has 60 minutes, so the total becomes -60 - 15 = -75 minutes.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment