开发者

Convert int val to format "HH:MM:SS" using JDK1.4

What is the most efficient method of converting an int val to its string开发者_如何学C counterpart in this format - "HH:MM:SS"

10 becomes 00:00:10
70 becomes 00:01:10
3610 becomes 01:00:10

I need this to be JDK1.4 compliant.

What I've come up with is a series of if statements and then constructing a string based on the current int val. This is not very efficient. Is there a better way?


Instead of a heap of code, how about just one line?

String time = new SimpleDateFormat("HH:mm:ss")  
    {{setTimeZone(TimeZone.getTimeZone("UTC"));}}.format(new Date(3610 * 1000)); // "01:00:10"

p.s. I never fails to amaze me just how much code some people write to do the simplest of things

The timezone is set to UTC, because SimpleDateFormat uses your locale to format the date. For example my timezone is GMT+10 and if I format new Date(0), the time part is "10:00:00".

For those who don't recognise that odd syntax where the timezone is being set, it's an anonymous class with an "instance block", which gets executed on construction, after the constructor has finished.


You need to clarify your definition of "efficient". To me efficient is "doesn't requiring to reinvent the wheel myself". Which means using libraries (yes, java.util.Calendar is quite outdated, but still better than coding it yourself):

int yourIntInSeconds = 3610;

Calendar dayStart = Calendar.getInstance();
dayStart.set(Calendar.HOUR_OF_DAY, 0);
dayStart.set(Calendar.MINUTE, 0);
dayStart.set(Calendar.SECOND, 0);
// also reset subsecond values if plan to display those

dayStart.add(Calendar.SECOND, yourIntInSeconds);
System.out.println(
   new SimpleDateFormat("HH:mm:ss").format(
      new Date(dayStart.getTimeInMillis())
   )
);

All of Calendar, Date and SimpleDateFormat are compatible with JDK 1.4.


This is what I do:

private static final String LONG_DURATION_FMT = "%d:%02d:%02d";
private static final String SHORT_DURATION_FMT = "%d:%02d";

public static String formatSecondsAsDuration(long seconds) {
    final long h = seconds / 3600;
    final long remainder = seconds % 3600;
    final long m = remainder / 60;
    final long s = remainder % 60;

    if (h > 0) {
        return String.format(LONG_DURATION_FMT, h, m, s);
    } else {
        return String.format(SHORT_DURATION_FMT, m, s);
    }
}

But using a StringBuffer instead of String.format could be faster. Just try it.


How about this (I made it in my last project):

public static String formatTime(long totalSeconds)
{
    int hours, minutes, remainder, totalSecondsNoFraction;
    double seconds;


    // Calculating hours, minutes and seconds
    String s = Double.toString(totalSeconds);
    String [] arr = s.split("\\.");
    totalSecondsNoFraction = Integer.parseInt(arr[0]);
    hours = totalSecondsNoFraction / 3600;
    remainder = totalSecondsNoFraction % 3600;
    minutes = remainder / 60;
    seconds = remainder % 60;
    if(arr[1].contains("E")) seconds = Double.parseDouble("." + arr[1]);
    else seconds += Double.parseDouble("." + arr[1]);


    // Formatting the string that conatins hours, minutes and seconds
    StringBuilder result = new StringBuilder(".");
    String sep = "", nextSep = " and ";
    if(seconds > 0)
    {
        result.insert(0, " seconds").insert(0, seconds);
        sep = nextSep;
        nextSep = ", ";
    }
    if(minutes > 0)
    {
        if(minutes > 1) result.insert(0, sep).insert(0, " minutes").insert(0, minutes);
        else result.insert(0, sep).insert(0, " minute").insert(0, minutes);
        sep = nextSep;
        nextSep = ", ";
    }
    if(hours > 0)
    {
        if(hours > 1) result.insert(0, sep).insert(0, " hours").insert(0, hours);
        else result.insert(0, sep).insert(0, " hour").insert(0, hours);
    }
    return result.toString();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜