SoFunction
Updated on 2025-04-09

Android method to convert seconds into hours, minutes and seconds

In converting time, seconds are usually converted into small functions of time, minute and second. How can it be done? In fact, it is simple. This involves the mutual conversion between time, minute and second.

The specific code is as follows:

import ;
public class ToolsUtil {
 private static ToolsUtil toolsUtil;
 private Context mContext;
 private ToolsUtil(Context context) {
  mContext = ();
 }
 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);
 }
}

This will convert the time into a time format of 00:00:00

PS: Let's take a look at how android converts it to time, minute and second

Convert seconds to hours, minutes and seconds

public static String cal(int second) {
    int h = 0;
    int d = 0;
    int s = 0;
    int temp = second % 3600;
    if (second &gt; 3600) {
      h = second / 3600;
      if (temp != 0) {
        if (temp &gt; 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 + "hour" + d + "point" + s + "Second";
  }

How many hours, minutes and seconds are obtained through seconds

public class TimeUtils {
  public static String getHours(long second) {// Calculate how many hours a second    long h = 00;
    if (second &gt; 3600) {
      h = second / 3600;
    }
    return h+"";
  }

  public static String getMins(long second) {// Calculate how many minutes there are in seconds    long d = 00;
    long temp = second % 3600;
    if (second &gt; 3600) {
      if (temp != 0) {
        if (temp &gt; 60) {
          d = temp / 60;
        }
      }
    } else {
      d = second / 60;
    }
    return d + "";
  }
  public static String getSeconds(long second) {// Calculate how many seconds there are in seconds    long s = 0;
    long temp = second % 3600;
    if (second &gt; 3600) {
      if (temp != 0) {
        if (temp &gt; 60) {
          if (temp % 60 != 0) {
            s = temp % 60;
          }
        } else {
          s = temp;
        }
      }
    } else {
      if (second % 60 != 0) {
        s = second % 60;
      }
    }
    return s + "";
  }

}

Summarize

This is the end of this article about how Android converts seconds into time and minute seconds. For more related content on Android conversions seconds into time and minute seconds, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!