Since, it's 2018, you really should be making use of the Date/Time libraries introduced in Java 8
String minutes = "160";
Duration duration = Duration.ofMinutes(Long.parseLong(minutes));
long hours = duration.toHours();
long mins = duration.minusHours(hours).toMinutes();
// Or if you're lucky enough to be using Java 9+
//String formatted = String.format("%dhrs %02dmins", duration.toHours(), duration.toMinutesPart());
String formatted = String.format("%dhrs %02dmins", hours, mins);
System.out.println(formatted);
Which outputs...
2hrs 40mins
Why use something like this? Apart of generally been a better API, what happens when minutes
equals something like 1600
?
Instead of printing 2hrs 40mins
, the above will display 26hrs 40mins
. SimpleDateFormat
formats date/time values, it doesn't deal with duration
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…