How to Format Seconds in Java? Last Updated : 08 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In Java, when working with time-related data, we may need to format seconds to make it a more human-readable representation. In this article, we will see how to format seconds in Java. Example of Input: long seconds = 3665;Output: Formatted Time: 1 hour, 1 minute and 5 seconds SyntaxThe formatting can be achieved using Duration and Period classes from java.time package introduced in Java 8. String formattedTime = Duration.ofSeconds(seconds) .toString() .substring(2) .replaceAll("(\\d[HMS])(?!$)", "$1 ") .toLowerCase();Program to Format Seconds in JavaBelow is the implementation of Format Seconds in Java: Java // Java program to format seconds import java.io.*; import java.time.Duration; public class GFG { // method to format seconds into HH:MM:SS public static String Seconds(long seconds) { // convert seconds to Duration object and format it return Duration.ofSeconds(seconds) .toString() // convert to string .substring(2) // remove "PT" prefix .replaceAll("(\\d[HMS])(?!$)", "$1 ") // add space between values .toLowerCase(); // convert to lowercase } public static void main(String[] args) { // test the Seconds method long seconds = 3665; String formattedTime = Seconds(seconds); System.out.println("Formatted Time: " + formattedTime); } } OutputFormatted Time: 1h 1m 5s Explanation of the Program:In the above program, Duration.ofSeconds(seconds): Creates a Duration object representing the given duration in the seconds.toString(): Converts the Duration object to its string representation.substring(2): The Removes the leading "PT" (Period of Time) from string.replaceAll("(\d[HMS])(?!$)", "$1 "): Adds a space between the numeric value and unit (H, M or S) to improve readability.toLowerCase(): Converts the string to lowercase for the consistent format. Comment More info S subramanyasmgm Follow Improve Article Tags : Java Java Programs Java-Date-Time Explore Java BasicsIntroduction to Java4 min readJava Programming Basics9 min readJava Methods7 min readAccess Modifiers in Java6 min readArrays in Java9 min readJava Strings8 min readRegular Expressions in Java7 min readOOP & InterfacesClasses and Objects in Java10 min readAccess Modifiers in Java6 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface11 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java6 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling8 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking15+ min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples8 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz | Level Up Your Java Skills1 min readTop 50 Java Project Ideas For Beginners and Advanced [Update 2025]15+ min read Like