如何将日期时间字符串转换为整数数据类型? [英] How can I convert a datetime string into an integer datatype?
问题描述
String timerStop1 = String.format("%02d", hours) + ":"+String.format("%02d", minutes) + ":"
+ String.format("%02d", seconds);
我正在使用上面的串联字符串值,我想将其转换为整数在我的Android应用程序中使用。我该怎么做?
I'm taking the above concatenated string value and I want to convert it into an integer to use in my android application. How do I do this?
推荐答案
第一步是将时间换成 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://stackoverflow.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.
这篇关于如何将日期时间字符串转换为整数数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!