Java 在Android中将字符串格式的给定时间转换为秒
假设时间是以MM:SS(ex-02:30)或HH:MM:SS字符串格式给出的。我们如何将这个时间转换为秒。试试这个Java 在Android中将字符串格式的给定时间转换为秒,java,android,Java,Android,假设时间是以MM:SS(ex-02:30)或HH:MM:SS字符串格式给出的。我们如何将这个时间转换为秒。试试这个 hours = totalSecs / 3600; minutes = (totalSecs % 3600) / 60; seconds = totalSecs % 60; timeString = String.format("%02d",seconds); 在您的例子中,您可以使用以下内容: String time = "02:30"; //mm:ss String[] u
hours = totalSecs / 3600;
minutes = (totalSecs % 3600) / 60;
seconds = totalSecs % 60;
timeString = String.format("%02d",seconds);
在您的例子中,您可以使用以下内容:
String time = "02:30"; //mm:ss
String[] units = time.split(":"); //will break the string up into an array
int minutes = Integer.parseInt(units[0]); //first element
int seconds = Integer.parseInt(units[1]); //second element
int duration = 60 * minutes + seconds; //add up our values
如果您想包含小时,只需修改上面的代码,将小时乘以3600,即一小时内的秒数。公共类时间到秒{
public class TimeToSeconds {
// given: mm:ss or hh:mm:ss or hhh:mm:ss, return number of seconds.
// bad input throws NumberFormatException.
// bad includes: "", null, :50, 5:-4
public static long parseTime(String str) throws NumberFormatException {
if (str == null)
throw new NumberFormatException("parseTimeString null str");
if (str.isEmpty())
throw new NumberFormatException("parseTimeString empty str");
int h = 0;
int m, s;
String units[] = str.split(":");
assert (units.length == 2 || units.length == 3);
switch (units.length) {
case 2:
// mm:ss
m = Integer.parseInt(units[0]);
s = Integer.parseInt(units[1]);
break;
case 3:
// hh:mm:ss
h = Integer.parseInt(units[0]);
m = Integer.parseInt(units[1]);
s = Integer.parseInt(units[2]);
break;
default:
throw new NumberFormatException("parseTimeString failed:" + str);
}
if (m<0 || m>60 || s<0 || s>60 || h<0)
throw new NumberFormatException("parseTimeString range error:" + str);
return h * 3600 + m * 60 + s;
}
// given time string (hours:minutes:seconds, or mm:ss, return number of seconds.
public static long parseTimeStringToSeconds(String str) {
try {
return parseTime(str);
} catch (NumberFormatException nfe) {
return 0;
}
}
}
import org.junit.Test;
import static org.junit.Assert.*;
public class TimeToSecondsTest {
@Test
public void parseTimeStringToSeconds() {
assertEquals(TimeToSeconds.parseTimeStringToSeconds("1:00"), 60);
assertEquals(TimeToSeconds.parseTimeStringToSeconds("00:55"), 55);
assertEquals(TimeToSeconds.parseTimeStringToSeconds("5:55"), 5 * 60 + 55);
assertEquals(TimeToSeconds.parseTimeStringToSeconds(""), 0);
assertEquals(TimeToSeconds.parseTimeStringToSeconds("6:01:05"), 6 * 3600 + 1*60 + 5);
}
@Test
public void parseTime() {
// make sure all these tests fail.
String fails[] = {null, "", "abc", ":::", "A:B:C", "1:2:3:4", "1:99", "1:99:05", ":50", "-4:32", "-99:-2:4", "2.2:30"};
for (String t: fails)
{
try {
long seconds = TimeToSeconds.parseTime(t);
assertFalse("FAIL: Expected failure:"+t+" got "+seconds, true);
} catch (NumberFormatException nfe)
{
assertNotNull(nfe);
assertTrue(nfe instanceof NumberFormatException);
// expected this nfe.
}
}
}
}
//给定:mm:ss或hh:mm:ss或hhh:mm:ss,返回秒数。
//输入错误会引发NumberFormatException。
//错误包括:“”,空,:50,5:-4
公共静态长解析时间(字符串str)引发NumberFormatException{
如果(str==null)
抛出新的NumberFormatException(“parseTimeString null str”);
if(str.isEmpty())
抛出新的NumberFormatException(“parseTimeString empty str”);
int h=0;
int m,s;
字符串单位[]=str.split(“:”);
断言(units.length==2 | | units.length==3);
开关(单位长度){
案例2:
//mm:ss
m=整数.parseInt(单位[0]);
s=整数.parseInt(单位[1]);
打破
案例3:
//hh:mm:ss
h=整数.parseInt(单位[0]);
m=整数.parseInt(单位[1]);
s=整数.parseInt(单位[2]);
打破
违约:
抛出新的NumberFormatException(“parseTimeString失败:“+str”);
}
如果(m60 | s60 | h
此代码段应支持HH:MM:SS(v将以秒为单位)或HH:MM(v将以分钟为单位)引用或使用JodaTime解析持续时间:另请检查hi,他已经有小时分钟和秒(HH:MM:SS)。他想将其转换为毫秒。首先正确阅读问题。然后answer@Signare他问“我们怎么能把这个时间转换成秒呢?”他没有说毫秒!分钟=(totalSecs%3600)/60;我在挣扎分钟部分。这句话解决了我的问题。谢谢。
private static final String TIME_FORMAT = "hh:mm a";//give whatever format you want.
//Function calling
long timeInMillis = TimeUtils.getCurrentTimeInMillis("04:21 PM");
long seconds = timeInMillis/1000;
//Util Function
public static long getCurrentTimeInMillis(String time) {
SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT, Locale.getDefault());
// sdf.setTimeZone(TimeZone.getTimeZone("GMT")); //getting exact milliseconds at GMT
// sdf.setTimeZone(TimeZone.getDefault());
Date date = null;
try {
date = sdf.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
return date.getTime();
}
int v = 0;
for (var x: t.split(":")) {
v = v * 60 + new Byte(x);
}