package org.springblade.common.utils;
|
|
import bsh.ParseException;
|
|
import java.text.SimpleDateFormat;
|
import java.util.Date;
|
|
/**
|
* 时间装换
|
* @author zhongrj
|
*/
|
public class TimeSwitchUtil {
|
public static void main(String[] args) {
|
String time1 = secondToTime(85959);
|
String time2 = dateToTime("2017/10/25 16:42:46", "yyyy/MM/dd HH:mm:ss");
|
System.out.println(time1);
|
System.out.println(time2);
|
}
|
|
/**
|
* 将秒数转换为日时分秒,
|
* @param second
|
* @return
|
*/
|
public static String secondToTime(long second){
|
//转换天数
|
long days = second / 86400000;
|
//剩余秒数
|
second = second % 86400000;
|
//转换小时
|
long hours = second / 3600000;
|
//剩余秒数
|
second = second % 3600000;
|
//转换分钟
|
long minutes = second /60000;
|
//剩余秒数
|
second = second % 60000;
|
//装换秒
|
long s = second /1000;
|
if(days>0){
|
return days + "天" + hours + "小时" + minutes + "分" + s + "秒";
|
}else{
|
return hours + "小时" + minutes + "分" + s + "秒";
|
}
|
}
|
|
/**
|
* 将日期转换为日时分秒
|
* @param date
|
* @return
|
*/
|
public static String dateToTime(String date, String dateStyle){
|
SimpleDateFormat format = new SimpleDateFormat(dateStyle);
|
Date oldDate = null;
|
try {
|
oldDate = format.parse(date);
|
} catch (java.text.ParseException e) {
|
e.printStackTrace();
|
}
|
//输入日期转换为毫秒数
|
long time = oldDate.getTime();
|
long nowTime = System.currentTimeMillis(); //当前时间毫秒数
|
long second = nowTime - time; //二者相差多少毫秒
|
second = second / 1000; //毫秒转换为妙
|
long days = second / 86400;
|
second = second % 86400;
|
long hours = second / 3600;
|
second = second % 3600;
|
long minutes = second /60;
|
second = second % 60;
|
if(days>0){
|
return days + "天" + hours + "小时" + minutes + "分" + second + "秒";
|
}else{
|
return hours + "小时" + minutes + "分" + second + "秒";
|
}
|
}
|
}
|