java时间比较

本文介绍如何在Java中比较两个日期的时间,并通过日期格式化将其转换为字符串进行比较。包括日期时间比较方法和日期格式化技巧。

方法一:判断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

转载于:https://my.oschina.net/zhangshuge/blog/633882

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值