How to convert Milliseconds to X mins, x seconds in Java?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Converting milliseconds to a more readable format like "minutes and seconds" is a common requirement in programming, especially when dealing with time intervals, animation durations, or event timings in applications. Java, with its rich API, provides various ways to handle such conversions efficiently.
Understanding Milliseconds
Milliseconds represent the thousandth part of a second. To convert them into minutes and seconds, we need to understand the basic time conversions:
- 1 second = 1000 milliseconds
- 1 minute = 60 seconds = 60,000 milliseconds
Using Basic Arithmetic
A straightforward way to convert milliseconds into minutes and seconds involves using simple arithmetic operations—division and modulus.
- Extract Minutes: Use division to find out how many whole minutes fit into the given milliseconds.
- Extract Seconds: Use the modulus operator to find out what remains after extracting minutes, then convert this remainder into seconds.
Here's a simple example in Java to demonstrate this method:
This function directly computes minutes and seconds from the total milliseconds, providing a clear, concise conversion. It's efficient and straightforward, suitable for many applications requiring basic time formatting.
Using java.time Package
For more complex time manipulations, Java 8 introduced the java.time package, which offers a modern approach to date and time. You can use this API for precise time calculations and formatting.
To convert milliseconds to minutes and seconds using java.time, you can use the following steps:
- Create a Duration Instance: Construct a
Durationobject from the milliseconds. - Extract Units: Use the methods
toMinutes()andtoSecondsPart()to get the minute and second components.
Here's how you can utilize the java.time API:
Comparing Both Methods
Both methods are effective, but the choice between them depends on your specific needs—simplicity versus precision and additional functionality.
| Method | Pros | Cons |
| Basic Arithmetic | Simple and straightforward | Limited to basic calculations |
java.time API | More precise and flexible | Slightly more complex, requires Java 8 or higher |
Additional Considerations
When converting time, consider the edge cases and precision requirements of your application. For example, handle cases where milliseconds are less than 1000 (all result in 0 mins, <1 sec), and ensure your application's performance when dealing with a large number of conversions.
Conclusion
Converting milliseconds to minutes and seconds in Java can be achieved through basic arithmetic for most needs or using the powerful java.time API for more complex scenarios. Choosing the right method depends on your application's requirements in terms of readability, ease of use, and the features offered by your Java version.
Related reading
- How to convert object array to string array in Java
- How to convert Set to Array?
- How to convert Set<String> to String[]?
- How to convert String object to Boolean Object?
- How to convert String to long in Java?
- How to convert Strings to and from UTF8 byte arrays in Java
- How to convert UTF-8 byte to string
- How to convert/parse from String to char in java?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.