方法一:判断date1是否在当前时间之前
(new Date().before(date1)
方法二:通过运算比较这样能算出差多少时间
public static void main(String[] args) {
Date d1 = new Date(); //第一个时间
Date d2 = new Date(); //第二个时间
SimpleDateFormat f = new SimpleDateFormat("hhmmss"); //格式化为 hhmmss
int d1Number = Integer.parseInt(f.format(d1).toString()); //将第一个时间格式化后转为int
int d2Number = Integer.parseInt(f.format(d2).toString()); //将第二个时间格式化后转为int
if(d1Number>d2Number){
System.out.println("时间d1大");
System.out.println(d1Number);
}
else{
System.out.println("时间d2大");
System.out.println(d2Number);
}
}
本文出自 “小浩” 博客,请务必保留此出处http://zhangchi.blog.51cto.com/5214280/1216589
本文介绍如何在Java中比较两个日期的时间,并通过日期格式化将其转换为字符串进行比较。包括日期时间比较方法和日期格式化技巧。

16万+

被折叠的 条评论
为什么被折叠?



