Method of converting seconds into time division seconds by Android

  • 2021-12-04 11:15:09
  • OfStack

In the conversion of time, the second is usually converted into a small function of minutes and seconds. How can we do it? In fact, it is also simple. This involves the mutual conversion between minutes and seconds

The specific code is as follows:


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);
 }
}

This converts the time to the time format of 00:00:00

ps: Let's take a look at android converted from seconds to minutes and seconds

Convert seconds into minutes and seconds


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 + " Hour " + d + " Points " + s + " Seconds ";
  }

How many hours, minutes and seconds are obtained by seconds


public class TimeUtils {
  public static String getHours(long second) {// How many hours are there in a second 
    long h = 00;
    if (second > 3600) {
      h = second / 3600;
    }
    return h+"";
  }

  public static String getMins(long second) {// How many minutes are there in a 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) {// How many seconds are there in counting seconds 
    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 + "";
  }

}

Summarize


Related articles: