本文主要是介绍计算两个时间之间差了几天几小时几分几秒,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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;}
}
这篇关于计算两个时间之间差了几天几小时几分几秒的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!