开发者

Convert simple time format to Special time format (hhmmt)

I'm trying to convert the time format (hh:mm:ss) to this Special Time Format (hhmmt)?

The Special Time Format (STF) is a 5 digit integer value with the开发者_Python百科 format [hhmmt] where hh are the hours, mm are the minutes and t is the tenth of a minute.

For example, 04:41:05 would convert to 4411.

I'm not sure how to convert the seconds value (05) to a tenth of a minute (1).

Edit:

I've incorporated Adithya's suggestion below to convert seconds to a tenth of a minute, but I'm still stuck.

Here is my current code:

String[] timeInt = time.split(":");
String hours = timeInt[0];
String minutes = timeInt[1];
double seconds = Double.parseDouble(timeInt[2]);

int t = (int) Math.round(seconds/6);

if (t>=10) {
    int min = Integer.parseInt(minutes);
    // min+=1;
    t = 0;
}

String stf="";
stf += hours;
stf += minutes;
stf += String.valueOf(t);

int stf2 = Integer.parseInt(stf);
return stf2;

I'm using a String to store the minutes value but it makes it difficult to increment it since it is a String and not a Integer. But when calculating the "t" (tenth of minute) I have to add 1 to minutes if it exceeds 10. If I were to use parseInt again it will exclude the 0 in front of minutes again.

How can I retain the leading zero and still increment the minutes?

Thanks.


I am guessing, its the value of seconds, compared to the tenth of a minute, which is 6 seconds. So formula for t would be

t= seconds/6 //rounded off to nearest integer

This makes sense as this value is always between 0 and 9, as seconds range from 0 and 59, so its always a single digit

For your example

 t = 5/6 = 0.833 = rounded off to 1


Note the 6.0f in the division - this helps you avoid the INT truncation.

string FormatSpecialTime(string time)
{
    if (time.Length != 8) throw YourException();

    int HH, mm, SS, t;
    if (!int.TryParse(time.Substring(0, 2), out HH)) HH = 0;
    if (!int.TryParse(time.Substring(0, 2), out mm)) mm = 0;
    if (!int.TryParse(time.Substring(0, 2), out SS)) SS = 0;

    t = (int) Math.Round(SS / 6.0f);

    if (t >= 10) 
    {
        mm++;
        t = 0;
    }
    if (mm >= 60) 
    {
        HH += mm / 60;
        mm = mm % 60;
    }

    return HH.ToString() + (mm > 10 ? mm.ToString() : @"0" + mm) + t;
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜