84 lines
2.5 KiB
Java
84 lines
2.5 KiB
Java
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("答错了");
|
||
// }
|
||
//}
|
||
}
|
||
}
|