public class Time { // format seconds to day hour minute seconds style // Exmplae 5000s will be formatted to 1h23m20s public static String toDhmsStyle(long allSeconds) { String DateTimes = null; long days = allSeconds / (60 * 60 * 24); long hours = (allSeconds % (60 * 60 * 24)) / (60 * 60); long minutes = (allSeconds % (60 * 60)) / 60; long seconds = allSeconds % 60; if (days > 0) { DateTimes = days + "d" + hours + "h" + minutes + "m" + seconds + "s"; } else if (hours > 0) { DateTimes = hours + "h" + minutes + "m" + seconds + "s"; } else if (minutes > 0) { DateTimes = minutes + "m" + seconds + "s"; } else { DateTimes = seconds + "s"; } return DateTimes; } public static void main(String[] args) { long mss = 5000; String ss = Time.toDhmsStyle(mss); System.out.println(ss); } }
输出:
1h23m20s
参考资料:
https://blog.csdn.net/weixin_36795952/article/details/78545982
https://blog.csdn.net/qq_22019789/article/details/74665791
--END-- 2019年10月5日15:25:39