Android的字符串变量(HH:MM)长变量毫秒 [英] Android String variable (HH:mm) to long variable miliseconds

查看:387
本文介绍了Android的字符串变量(HH:MM)长变量毫秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串变量这种格式HH:MM
比方说,该变量的值是05:30。

I have a String variable in this format "HH:mm". Lets say the variable value is 05:30.

我将如何能够从字符串获取这些数字并计算:

How would I be able to get those numbers from string and calculate :

(05 * 60 * 60 * 1000)+(30 * 60 * 1000),并放入一个新的长期变量数(毫秒)。

(05 * 60 * 60 * 1000) + (30 * 60 * 1000) and put that number (miliseconds) in a new long variable.

基本上我需要字符串时间变量转换成毫秒长变量。

Basically I need to convert the String time variable into miliseconds long variable.

推荐答案

取值包含感兴趣的字符串。然后,例如你可以说

Let s contain the string of interest. Then e.g. you can say

    String s = "5:30";
    Pattern p = Pattern.compile("(\\d+):(\\d+)");
    Matcher m = p.matcher(s);
    if (m.matches()) {
        int hrs = Integer.parseInt(m.group(1));
        int min = Integer.parseInt(m.group(2));
        long ms = (long) hrs * 60 * 60 * 1000 + min * 60 * 1000;
        System.out.println("hrs="+hrs+" min="+min+" ms="+ms);
    } else {
        throw Exception("Bad time format");
    }

有关它的价值,只有86,400,000一天毫秒,所以你不需要。一个 INT 是足够大的。

For what it's worth, there are only 86,400,000 milliseconds in a day, so you don't need the long. An int is big enough.

这篇关于Android的字符串变量(HH:MM)长变量毫秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆