Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将日期时间字符串转换为整数数据类型?_Java_String_Date_Integer - Fatal编程技术网

Java 如何将日期时间字符串转换为整数数据类型?

Java 如何将日期时间字符串转换为整数数据类型?,java,string,date,integer,Java,String,Date,Integer,我取上面连接的字符串值,我想把它转换成一个整数,在我的android应用程序中使用。如何执行此操作?第一步是将HH:MM:SS格式的时间(即字符串的格式)转换为秒,如下所示: String timerStop1 = String.format("%02d", hours) + ":"+String.format("%02d", minutes) + ":" + String.format("%02d", seconds); 但是,这只获取秒(整数)形式的时间

我取上面连接的字符串值,我想把它转换成一个整数,在我的android应用程序中使用。如何执行此操作?

第一步是将HH:MM:SS格式的时间(即字符串的格式)转换为秒,如下所示:

String timerStop1 = String.format("%02d", hours) + ":"+String.format("%02d", minutes) + ":"
                  + String.format("%02d", seconds);
但是,这只获取秒(整数)形式的时间,而不是时间戳

更新:

而且,正如科林指出的,既然你已经可以访问变量:小时、分钟、秒——为什么不按照他的建议去做呢——这是完全正确的


这是因为OP想知道如何将HH:MM:SS字符串转换为整数——如果是这样,那么这是最常用的转换方法,我想。

不可能将
timerStop1
解析为整数,因为
timerStop1
包含数字以外的字符。

你能只使用变量hours,minutes,秒。假设通过转换为整数,您需要总秒数

String timerStop1 = String.format("%02d", hours) + ":" + String.format("%02d", minutes) + ":" + String.format("%02d", seconds);
String[] timef=timerStop1.split(":");  

int hour=Integer.parseInt(timef[0]);  
int minute=Integer.parseInt(timef[1]);  
int second=Integer.parseInt(timef[2]);  

int temp;  
temp = second + (60 * minute) + (3600 * hour);  

System.out.println("seconds " + temp); 
tostring()
方法不是必需的

int time = seconds + (minutes * 60) + (hours * 3600);

整数.parseInt(您的字符串);转换成整数是指总的秒数吗?你能告诉我们整数的用途吗?
timerStop1
类似于“10:30:12”。如果使用
integer.parseInt
将其转换为整数,则会出现NumberFormatException。因此,您需要说明如何定义此字符串到整数的转换。@colingilespie int用于计算速度。不可能,答案将不正确。如果您有权访问变量,为什么不使用我的解决方案。但我想如果你只能使用字符串,这是唯一的方法。@colingilespie,我同意-这也可能是一个从时间字符串转换的练习。但是,由于OP请求:
串联字符串值,并且我想将其转换为整数
,这是您能得到的最一般的结果(给定所述参数)。您能告诉我我在ansewer中更改什么以获得分钟吗?如果您想以分钟为单位查找时间,请更改包含以下内容的行:
temp=second+(60*minute)+(3600*hour);
到这个:
temp=minute+(60*hour);
为什么没有秒?因为在HH:MM:SS格式中,秒总是小于60。而且,如果我们要将秒从HH:MM:SS字符串转换为分钟,它总是0整数分钟-这就是我没有包含它的原因。
Integer.parseInt(timerStop1);