且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-01-30 18:07:50

第一步是将时间换成 HH:MM:SS 格式(这是你的字符串的格式)到秒的格式:

A first step will be to convert the time in HH:MM:SS format (which is how your string is formatted) to that of seconds as per the following:

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); 

但是,这只会将时间作为秒(整数),而不是时间戳!

更新:

而且,正如科林指出的那样,鉴于您已经拥有访问权限变量:小时,分钟,秒 - 为什么不像他建议的那样 - 这是完全正确的?

And, as Colin pointed out, given that you already have access to the variables: hours, minutes, seconds - why not do it like what he suggested - which is completely correct?

https://***.com/a/15307211/866930

那是因为OP想要知道如何将HH:MM:SS字符串转换为整数 - 如果是这样,那么这是最通用的方式,IMO。

That's because the OP wants to know how to convert an HH:MM:SS string to an integer - if so, then this is the most general way in which to do so, IMO.