How to convert hh:mm:ss to minutes in Java

1 Answer

0 votes
public class TimeConverter {
    public static double hhmmsstominutes(String hhmmss) {
        String[] timeParts = hhmmss.split(":");
        
        int hours = Integer.parseInt(timeParts[0]);
        int minutes = Integer.parseInt(timeParts[1]);
        int seconds = Integer.parseInt(timeParts[2]);

        return (hours * 60) + minutes + (seconds / 60.0);
    }

    public static void main(String[] args) {
        System.out.println(hhmmsstominutes("2:30:00"));
        System.out.println(hhmmsstominutes("2:35:30"));
        System.out.println(hhmmsstominutes("5:00:45"));
    }
}


/*
run:

150.0
155.5
300.75

*/

 



answered Apr 17, 2025 by avibootz

Related questions

1 answer 58 views
1 answer 84 views
1 answer 109 views
1 answer 92 views
1 answer 106 views
1 answer 96 views
1 answer 179 views
...