0

我正在尝试将时间格式 (hh:mm:ss) 转换为这种特殊时间格式 (hhmmt)?

特殊时间格式 (STF) 是一个 5 位整数值,格式为 [hhmmt],其中 hh 是小时,mm 是分钟,t 是十分之一分钟。

例如,04:41:05 将转换为 4411。

我不确定如何将秒值 (05) 转换为十分之一分钟 (1)。

编辑:

我在下面合并了 Adithya 的建议,将秒数转换为十分之一分钟,但我仍然卡住了。

这是我当前的代码:

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;

我使用字符串来存储分钟值,但由于它是字符串而不是整数,因此很难增加它。但是在计算“t”(十分之一分钟)时,如果超过 10,我必须将 1 添加到分钟。如果我要再次使用 parseInt,它将再次排除分钟前面的 0。

如何保留前导零并仍然增加分钟?

谢谢。

4

2 回答 2

0

请注意除法中的 6.0f - 这可以帮助您避免 INT 截断。

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;
}
于 2012-04-29T13:12:46.140 回答
0

我猜,它是秒的值,与十分之一分钟相比,即 6 秒。所以 t 的公式是

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

这是有道理的,因为这个值总是在 0 到 9 之间,因为秒的范围是 0 到 59,所以它总是一个数字

对于你的例子

 t = 5/6 = 0.833 = rounded off to 1
于 2011-08-25T08:20:44.667 回答