import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 计算出两个时间之间相差了几天几小时几分钟几秒
* @author zjhn-llq
* @date 2019/10/19 11:06
*/
public class translateTimeUtil {
public static String translateTime(Date start, Date end) {
if (start == null || end == null) {
return "";
}
long time = end.getTime() - start.getTime();
long days = time / (1000 * 60 * 60 * 24);
long hours = (time - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
long minutes = (time - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
long seconds = (time - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60) - minutes * (1000 * 60)) / 1000;
String descript = "";
if (days > 0) {
descript = descript + (days + "天");
}
if (hours > 0) {
descript = descript + (hours + "小时");
}
if (minutes > 0) {
descript = descript + (minutes + "分");
}
if (seconds > 0) {
descript = descript + (seconds + "秒");
}
return descript;
}
/**
* string类型转换为date类型
* @param dateStr string类型的时间
* @param pattern 转换的时间模式 yyyy-MM-dd HH:mm:ss
* @return
* @throws ParseException
*/
public static Date stringToDate(String dateStr, String pattern) throws ParseException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
Date date = simpleDateFormat.parse(dateStr);
return date;
}
}