简体   繁体   English

Android(Java)将hh:mm:ss时间字符串转换为数字表示形式

[英]Android (Java) convert a hh:mm:ss time String into a number representation

The problem i am having is that i have a time string which is represented in terms of "hh:mm:ss" and the server only accepts float numerical for the time (this is beyond my control). 我遇到的问题是我有一个用“ hh:mm:ss”表示的时间字符串,并且服务器仅接受该时间的浮点数字(这超出了我的控制范围)。

Currently i am removing all ':' characters from the String, so for example "11:23:23" becomes "112323". 目前,我正在从字符串中删除所有“:”字符,因此“ 11:23:23”变为“ 112323”。

With the "new" String i can then convert it to a number representation for example: 然后,使用“新”字符串,我可以将其转换为数字表示形式:

int x = Integer.parseInt("112323); int x = Integer.parseInt(“ 112323);

This will produce the integer value 112323. 这将产生整数值112323。

Now, my problem is because it is a timer, sometimes the time represented in the String is quite low. 现在,我的问题是因为它是一个计时器,所以有时String中表示的时间很短。 If i had the time string "00:00:01" and removed the ':' characters it would produce the integer value 1. 如果我的时间字符串为“ 00:00:01”并删除了“:”字符,它将产生整数值1。

This is because if am not mistaken, the integer value 000001 will be "trimmed" as the 0's do not change the value of the integer so the value 1 is returned. 这是因为,如果没有记错,则整数值000001将被“修剪”,因为0不会改变整数的值,因此将返回值1。

Is there a way to specify that i want to display the full 6 digits regardless of the fact they maybe "insignificant" in terms of the number value? 有没有一种方法可以指定我要显示完整的6位数字,而不考虑它们在数字值方面可能“无关紧要”的事实?

my code: 我的代码:

String timeTrimmed = pois.get(i).getTimeStamp().replaceAll(":","");
                int result = Integer.parseInt(timeTrimmed);

is it possible to do something like this: 是否可以做这样的事情:

String timeTrimmed = pois.get(i).getTimeStamp().replaceAll(":","");
                int result = Integer.parseInt(######,timeTrimmed);

with the # representing the fact i want the integer to keep all the digits? 用#表示事实,我希望整数保留所有数字?

I hope you understand what i mean, 我希望你明白我的意思,

Cheers, Chris. 干杯,克里斯。

Why not split the sting on ":"? 为什么不分割“:”上的字符串?
Now you'd have an array with the strings representing the hours, the minutes and the seconds. 现在,您将拥有一个数组,其中的字符串表示小时,分钟和秒。
Parse all of them to ints. 将它们全部解析为整数。
Multiply the minutes by 60 and the hours by 3600. 分钟乘以60,小时乘以3600。
Sum the three parts and you have the time in seconds (integer). 对这三个部分求和,您得到的时间以秒为单位(整数)。

On the other side of the net, decoding seconds to an "hh:mm:ss" formatted string would be a trivial task. 在网络的另一端,将秒解码为“ hh:mm:ss”格式的字符串将是一件微不足道的任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM