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

2019-08-02 02:16发布

What is the most efficient method of converting an int val to its string 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?

4条回答
Deceive 欺骗
2楼-- · 2019-08-02 02:36

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();
}
查看更多
淡お忘
3楼-- · 2019-08-02 02:37

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.

查看更多
乱世女痞
4楼-- · 2019-08-02 02:45

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.

查看更多
爷的心禁止访问
5楼-- · 2019-08-02 02:56

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.

查看更多
登录 后发表回答