1. 程式人生 > >時分秒和秒相互轉換

時分秒和秒相互轉換

文章目錄

1、簡介

實現將時分秒 :**格式 轉化成 秒
實現將 秒 轉化成 時分秒

在這裡插入圖片描述

2、程式碼結構

在這裡插入圖片描述

3、activity_main.xml 佈局檔案

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/edit_sec_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="輸入數字"
        android:textSize="30dp"/>

    <TextView
        android:id="@+id/text_hms_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="點選轉化時分秒"
        android:textSize="30dp"/>

    <EditText
        android:layout_marginTop="80dp"
        android:id="@+id/edit_hms_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="01:23:45"
        android:textSize="30dp"/>

    <TextView
        android:id="@+id/edit_log_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="點選轉化數字"
        android:textSize="30dp"/>

</LinearLayout>

4、MainActivity.java 功能檔案

package com.example.tssh.mysectohms;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private String TAG  = "MainActivity: ";
    private EditText editTextOne, editTextTwo;
    private TextView textViewOne,textViewTwo;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editTextOne = (EditText) findViewById(R.id.edit_sec_id);
        textViewOne = (TextView) findViewById(R.id.text_hms_id);
        textViewOne.setOnClickListener(this);
        editTextTwo = (EditText) findViewById(R.id.edit_hms_id);
        textViewTwo = (TextView) findViewById(R.id.edit_log_id);
        textViewTwo.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.text_hms_id:
                String longData = editTextOne.getText().toString();  //獲取輸入的數字
                secToTime(Long.parseLong(longData));// 將 miao 轉化成 **:**:** 格式
                break;
            case R.id.edit_log_id:
                String strTime = editTextTwo.getText().toString();//獲取時分秒 **:**:**
                formatTurnSecond(strTime);
                break;
        }
    }

    public  String secToTime(long time) {
        String timeStr = null;
        long hour = 0;
        long minute = 0;
        long second = 0;

        if (time <= 0)
            return "00:00:00";
        else {
            minute = time / 60;
            if (minute < 60) {
                second = time % 60;
            } else {
                hour = minute / 60;
                if (hour > 99)
                    return "99:59:59";
                minute = minute % 60;
                second = time - hour * 3600 - minute * 60;
            }
            timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
        }
        textViewOne.setText(timeStr);
        Log.i(TAG,"轉化的數字是:" + time + "\n"
                + "轉化後的時間格式是:" + timeStr);
        return timeStr;
    }

    public static String unitFormat(long i) {
        String retStr = null;
        if (i >= 0 && i < 10)
            retStr = "0" + Long.toString(i);
        else
            retStr = "" + i;
        return retStr;
    }


    /*
     * 將時分秒轉為秒數
     * */
    public long formatTurnSecond(String time) {
        String s = time;
        int index1 = s.indexOf(":");
        int index2 = s.indexOf(":", index1 + 1);
        int hh = Integer.parseInt(s.substring(0, index1));
        int mi = Integer.parseInt(s.substring(index1 + 1, index2));
        int ss = Integer.parseInt(s.substring(index2 + 1));
        long timeSec = hh * 60 * 60 + mi * 60 + ss;

        Log.e(TAG, "傳進來的時分秒:" + time + "\n" + " 轉化的秒是 == " + hh * 60 * 60 + mi * 60 + ss);
        textViewTwo.setText(timeSec + "");
        return timeSec;
    }
}

5、log 展示

在這裡插入圖片描述

文獻參考:
Java中整數(秒數)轉換為時分秒格式(xx:xx:xx)
https://blog.csdn.net/zuolongsnail/article/details/8167606

Android 倒計時; 時分秒與秒數的互轉
https://my.oschina.net/lijindou/blog/798710