欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-21 23:04:03
在对时间进行转换中,通常会把秒转换成时分秒的小功能,怎么才能做到呢,其实也简单 这就涉及到时分秒之间的相互转换具体代码如下:import android.content.context;public...

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

具体代码如下:

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