当前位置: 移动技术网 > IT编程>移动开发>Android > Android 实现秒转换成时分秒的方法

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

2020年06月18日  | 移动技术网IT编程  | 我要评论

魔术奇缘片尾曲,2013驾照模拟考试c1,父子tck车震

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

具体代码如下:

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 秒转换成时分秒内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网