2024-02-20 14:26:43 +08:00

84 lines
2.5 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.zhgd.xmgl.util;
import java.util.Objects;
import java.util.Scanner;
public class TimeUtil {
/**
* 获取时分秒显示格式548h17min31s
*
* @param second
* @return
*/
public static String toHourMinSecond(Integer second) {
if (second == null) {
return null;
}
int h = second / 3600;
int min = (second % 3600) / 60;
int s = (second % 3600) % 60;
String rs = "";
if (second >= 3600) {
rs = h + "h";
}
if (second >= 60) {
rs += min + "min";
}
return rs + s + "s";
}
/**
* 获取天时分秒中文显示,格式:
*
* @param second
* @return
*/
public static String toDayHourMinSecond(Long second) {
if (second == null) {
return null;
}
long day = second / 86400;
long h = (second % 86400) / 3600;
long min = (second % 3600) / 60;
long s = (second % 3600) % 60;
String rs = "";
if (second >= 86400) {
rs = day + "";
}
if (second >= 3600) {
rs += h + "";
}
if (second >= 60) {
rs += min + "";
}
return rs + s + "";
}
public static void main(String[] args) {
System.out.println(toDayHourMinSecond(1L));
System.out.println(toDayHourMinSecond(10L));
System.out.println(toDayHourMinSecond(60L));
System.out.println(toDayHourMinSecond(360L));
System.out.println(toDayHourMinSecond(3600L));
System.out.println(toDayHourMinSecond(13600L));
System.out.println(toDayHourMinSecond(113600L));
System.out.println(toDayHourMinSecond(1113600L));
System.out.println(toDayHourMinSecond(11113600L));
System.out.println(toDayHourMinSecond(111113600L));
System.out.println(toDayHourMinSecond(1111113600L));
//abcdefg hijkl mnopq rst uvwxyz
//for (int i = 0; i < 100; i++) {
// Scanner scanner = new Scanner(System.in);
// char c1 = (char) ('a' + Math.random() * ('z' - 'a' + 1));
// char c2 = (char) ('a' + Math.random() * ('z' - 'a' + 1));
// System.out.println(c1 + ">>>" + c2);
// String read = scanner.nextLine();
// String rs = c1 < c2 ? "y" : "n";
// if (!Objects.equals(read, rs)) {
// System.out.println("答错了");
// }
//}
}
}