溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android如何實現(xiàn)秒轉(zhuǎn)換成時分秒

發(fā)布時間:2020-07-21 13:52:11 來源:億速云 閱讀:654 作者:小豬 欄目:移動開發(fā)

這篇文章主要為大家展示了Android如何實現(xiàn)秒轉(zhuǎn)換成時分秒,內(nèi)容簡而易懂,希望大家可以學(xué)習(xí)一下,學(xué)習(xí)完之后肯定會有收獲的,下面讓小編帶大家一起來看看吧。

在對時間進行轉(zhuǎn)換中,通常會把秒轉(zhuǎn)換成時分秒的小功能,怎么才能做到呢,其實也簡單 這就涉及到時分秒之間的相互轉(zhuǎn)換

具體代碼如下:

import android.content.Context;
public class ToolsUtil {
 private static ToolsUtil toolsUtil;
 private Context mContext;
 private ToolsUtil(Context context) {
  mContext = context.getApplicationContext();
 }
 public static ToolsUtil getInstance(Context context) {
  if (toolsUtil == null) {
   toolsUtil = new ToolsUtil(context);
  }
  return toolsUtil;
 }
 public String timeConversion(int time) {
  int hour = 0;
  int minutes = 0;
  int sencond = 0;
  int temp = time % 3600;
  if (time > 3600) {
   hour = time / 3600;
   if (temp != 0) {
    if (temp > 60) {
     minutes = temp / 60;
     if (temp % 60 != 0) {
      sencond = temp % 60;
     }
    } else {
     sencond = temp;
    }
   }
  } else {
   minutes = time / 60;
   if (time % 60 != 0) {
    sencond = time % 60;
   }
  }
  return (hour<10&#63;("0"+hour):hour) + ":" + (minutes<10&#63;("0"+minutes):minutes) + ":" + (sencond<10&#63;("0"+sencond):sencond);
 }
}

這樣就把時間轉(zhuǎn)換成 00:00:00 的時間格式了

ps:下面看下android通過秒換算成時分秒

把秒換算成時分秒

public static String cal(int second) {
    int h = 0;
    int d = 0;
    int s = 0;
    int temp = second % 3600;
    if (second > 3600) {
      h = second / 3600;
      if (temp != 0) {
        if (temp > 60) {
          d = temp / 60;
          if (temp % 60 != 0) {
            s = temp % 60;
          }
        } else {
          s = temp;
        }
      }
    } else {
      d = second / 60;
      if (second % 60 != 0) {
        s = second % 60;
      }
    }
    return h + "時" + d + "分" + s + "秒";
  }

通過秒分別得出多少小時多少分多少秒

public class TimeUtils {
  public static String getHours(long second) {//計算秒有多少小時
    long h = 00;
    if (second > 3600) {
      h = second / 3600;
    }
    return h+"";
  }

  public static String getMins(long second) {//計算秒有多少分
    long d = 00;
    long temp = second % 3600;
    if (second > 3600) {
      if (temp != 0) {
        if (temp > 60) {
          d = temp / 60;
        }
      }
    } else {
      d = second / 60;
    }
    return d + "";
  }
  public static String getSeconds(long second) {//計算秒有多少秒
    long s = 0;
    long temp = second % 3600;
    if (second > 3600) {
      if (temp != 0) {
        if (temp > 60) {
          if (temp % 60 != 0) {
            s = temp % 60;
          }
        } else {
          s = temp;
        }
      }
    } else {
      if (second % 60 != 0) {
        s = second % 60;
      }
    }
    return s + "";
  }

}

以上就是關(guān)于Android如何實現(xiàn)秒轉(zhuǎn)換成時分秒的內(nèi)容,如果你們有學(xué)習(xí)到知識或者技能,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI