如何转换此字符串";03:34:21“;在Java中转换为秒

如何转换此字符串";03:34:21“;在Java中转换为秒,java,android,Java,Android,在我的应用程序中,我有一个计时器,每秒更新一个文本视图,格式如下: 00:00:00 - hours, minutes, seconds. 在onPause中,我将tvTimer的值作为字符串记录在SharedReference中,例如00:21:13(21分13秒)和onPause发生的时间 在onResume中,我获取这些值,并尝试将从现在到上一次onPause之间的时间添加到tvTimer字符串的值中,您可以在TODO中看到这一点,但我不知道如何做到这一点 private long st

在我的应用程序中,我有一个计时器,每秒更新一个文本视图,格式如下:

00:00:00 - hours, minutes, seconds.
在onPause中,我将tvTimer的值作为字符串记录在SharedReference中,例如00:21:13(21分13秒)和onPause发生的时间

在onResume中,我获取这些值,并尝试将从现在到上一次onPause之间的时间添加到tvTimer字符串的值中,您可以在TODO中看到这一点,但我不知道如何做到这一点

private long startTime = 0L;
private Handler customHandler = new Handler();
long timeInMilliseconds = 0L;
long timeSwapBuff = 0L;
long updatedTime = 0L;

private void startTimer() {
    tvTimer.setVisibility(View.VISIBLE);
    tvTimer.setText(obtainTimerProgress());

    startTime = SystemClock.uptimeMillis();
    customHandler.postDelayed(updateTimerThread, 0);
}

private Runnable updateTimerThread = new Runnable() {

    public void run() {

        timeInMilliseconds = SystemClock.uptimeMillis() - startTime;

        updatedTime = timeSwapBuff + timeInMilliseconds;

        int secs = (int) (updatedTime / 1000);
        int mins = secs / 60;
        secs = secs % 60;

        int hours = mins / 60;

        tvTimer.setText(String.format("%02d", hours) + ":" + String.format("%02d", mins) + ":" + String.format("%02d", secs));
        customHandler.postDelayed(this, 0);
        // TODO Find a suitable moment to customHandler.removeCallbacks
    }

};

private String obtainTimerProgress() {
    SharedPreferences preferences = getActivity().getSharedPreferences(Constant.USER_PREFERENCES, Activity.MODE_PRIVATE);
    String timerProgress = preferences.getString(Constant.PROGRESS + getUserId(), "00:00:00");
    if (timerProgress != null && !timerProgress.contentEquals("") && !timerProgress.contentEquals("00:00:00")) {
        long timeOfTimerPause = preferences.getLong(Constant.TIME_OF_ON_PAUSE + getUserId(), 0);
        long now = new Date().getTime();
        long differenceBetweenThenAndNow = (now - timeOfTimerPause ) / 1000;
        // TODO Parse the string "00:43:34" and turn it into seconds, add differenceBetweenThenAndNow and format it as "00:53:22" again
    }
    return timerProgress;
}
public class HelloWorld{ public static void main(String []args){ String time = "1:02:59"; String timeSplit[] = time.split(":"); int seconds = Integer.parseInt(timeSplit[0]) * 60 * 60 + Integer.parseInt(timeSplit[1]) * 60 + Integer.parseInt(timeSplit[2]); System.out.println(seconds); } } 在onPause中,我将tvTimer的值作为字符串记录在SharedReference中,例如00:21:13(21分13秒)和onPause发生的时间

在onResume中,我获取这些值,并尝试将从现在到上一次onPause之间的时间添加到tvTimer字符串的值中,您可以在TODO中看到这一点,但我不知道如何做到这一点。

公共类HelloWorld{
private long startTime = 0L;
private Handler customHandler = new Handler();
long timeInMilliseconds = 0L;
long timeSwapBuff = 0L;
long updatedTime = 0L;

private void startTimer() {
    tvTimer.setVisibility(View.VISIBLE);
    tvTimer.setText(obtainTimerProgress());

    startTime = SystemClock.uptimeMillis();
    customHandler.postDelayed(updateTimerThread, 0);
}

private Runnable updateTimerThread = new Runnable() {

    public void run() {

        timeInMilliseconds = SystemClock.uptimeMillis() - startTime;

        updatedTime = timeSwapBuff + timeInMilliseconds;

        int secs = (int) (updatedTime / 1000);
        int mins = secs / 60;
        secs = secs % 60;

        int hours = mins / 60;

        tvTimer.setText(String.format("%02d", hours) + ":" + String.format("%02d", mins) + ":" + String.format("%02d", secs));
        customHandler.postDelayed(this, 0);
        // TODO Find a suitable moment to customHandler.removeCallbacks
    }

};

private String obtainTimerProgress() {
    SharedPreferences preferences = getActivity().getSharedPreferences(Constant.USER_PREFERENCES, Activity.MODE_PRIVATE);
    String timerProgress = preferences.getString(Constant.PROGRESS + getUserId(), "00:00:00");
    if (timerProgress != null && !timerProgress.contentEquals("") && !timerProgress.contentEquals("00:00:00")) {
        long timeOfTimerPause = preferences.getLong(Constant.TIME_OF_ON_PAUSE + getUserId(), 0);
        long now = new Date().getTime();
        long differenceBetweenThenAndNow = (now - timeOfTimerPause ) / 1000;
        // TODO Parse the string "00:43:34" and turn it into seconds, add differenceBetweenThenAndNow and format it as "00:53:22" again
    }
    return timerProgress;
}
public class HelloWorld{ public static void main(String []args){ String time = "1:02:59"; String timeSplit[] = time.split(":"); int seconds = Integer.parseInt(timeSplit[0]) * 60 * 60 + Integer.parseInt(timeSplit[1]) * 60 + Integer.parseInt(timeSplit[2]); System.out.println(seconds); } } 公共静态void main(字符串[]args){ 字符串时间=“1:02:59”; 字符串timeSplit[]=time.split(“:”); int seconds=Integer.parseInt(timeSplit[0])*60*60+Integer.parseInt(timeSplit[1])*60+Integer.parseInt(timeSplit[2]); 系统输出打印项次(秒); } } 使用:拆分字符串,然后获取每个值并转换为秒。我已经用粗体标出了这些行。希望这有帮助

公共类HelloWorld{ 公共静态void main(字符串[]args){ 字符串时间=“1:02:59”; 字符串timeSplit[]=time.split(“:”); int seconds=Integer.parseInt(timeSplit[0])*60*60+Integer.parseInt(timeSplit[1])*60+Integer.parseInt(timeSplit[2]); 系统输出打印项次(秒); } }
使用:拆分字符串,然后获取每个值并转换为秒。我已经用粗体标出了这些行。希望这有帮助

在javadoc中查找
String.split
。还有
Integer.parseInt
。这给了你一些想法吗?顺便说一句:使用一个
格式
调用比使用三个调用+笨拙的字符串连接更简单/更干净/更有效。要构建/解析日期,你可以使用SimpleDataFormat。在javadoc中查找
字符串.split
。还有
Integer.parseInt
。这给了你一些想法吗?顺便说一句:使用一个
格式
调用比使用3个调用+笨拙的字符串连接更简单/更干净/更有效。要构建/解析日期,可以使用SimpleDateFormat。