Android如何实现秒转换成时分秒

发布时间:2020-07-21 13:52:11 作者:小猪
来源:亿速云 阅读:634

这篇文章主要为大家展示了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&#63;("0"+hour):hour) + ":" + (minutes<10&#63;("0"+minutes):minutes) + ":" + (sencond<10&#63;("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如何实现秒转换成时分秒的内容,如果你们有学习到知识或者技能,可以把它分享出去让更多的人看到。

推荐阅读:
  1. python将时分秒转换成秒的实例
  2. Android如何实现欢迎界面停留3秒效果

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android roi 如何实现

上一篇:网页布局方式如何使用清除浮动

下一篇:获取设备的主机名和ip地址

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》