Sometimes Java almost seems to go out of its way to make coding difficult, while Python goes out of its way to make it easy. Here is a case in point.
I needed a Java method that accepts a duration expressed as a number of minutes in a string, and returns the same duration formatted as HH:MM:SS. In Python, this is trivial:
def formatDuration(durStr):
hours, minutes = divmod(int(durStr), 60)
return '%02d:%02d:00' % (hours, minutes)
Java, on the other hand, makes me jump through hoops to do the same thing:
static String formatDuration(String spcsfDur) {
int duration = Integer.parseInt(spcsfDur);
int minutes = duration % 60;
int hours = (duration - minutes) / 60;
Object[] args = new Object[] {
new Integer(hours),
new Integer(minutes)
};
String durStr = MessageFormat.format("{0,number,00}:{1,number,00}:00", args);
return durStr;
}
Ouch.
|