华域联盟 Andriod Android 实现秒转换成时分秒的方法

Android 实现秒转换成时分秒的方法

在对时间进行转换中,通常会把秒转换成时分秒的小功能,怎么才能做到呢,其实也简单 这就涉及到时分秒之间的相互转换

具体代码如下:

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?("0"+hour):hour) + ":" + (minutes<10?("0"+minutes):minutes) + ":" + (sencond<10?("0"+sencond):sencond);
 }
}

这样就把时间转换成 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 + "";
  }

}

总结

到此这篇关于Android 实现秒转换成时分秒的方法的文章就介绍到这了,更多相关Android 秒转换成时分秒内容请搜索华域联盟以前的文章或继续浏览下面的相关文章希望大家以后多多支持华域联盟!

本文由 华域联盟 原创撰写:华域联盟 » Android 实现秒转换成时分秒的方法

转载请保留出处和原文链接:https://www.cnhackhy.com/108556.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

Android实现面包屑功能的代码(支持Fragment联动)

Flutter 使用Navigator进行局部跳转页面的方法

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们