Java 比较两个日期的方法

三种方法分别是:
1) by Using classic CompareTo method of Date class.
2) by using equals(), before() and after method of Date class.
3) by using equals(), before() and after method of Calendar class in Java.
标签: <无>

代码片段(2)

[代码] [Java]代码

view source
print ?
01import java.text.DateFormat;
02import java.text.ParseException;
03import java.text.SimpleDateFormat;
04import java.util.Calendar;
05import java.util.Date;
06  
07public class HashtableDemo {
08  
09     public static void main(String args[]) throws AssertionError, ParseException {
10  
11         DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
12  
13         //comparing date using compareTo method in Java
14         System.out.println("Comparing two Date in Java using CompareTo method");
15         compareDatesByCompareTo(df, df.parse("01-01-2012"), df.parse("01-01-2012"));
16         compareDatesByCompareTo(df, df.parse("02-03-2012"), df.parse("04-05-2012"));
17         compareDatesByCompareTo(df, df.parse("02-03-2012"), df.parse("01-02-2012"));
18  
19         //comparing dates in java using Date.before, Date.after and Date.equals
20         System.out.println("Comparing two Date in Java using Date's before, after and equals method");
21         compareDatesByDateMethods(df, df.parse("01-01-2012"), df.parse("01-01-2012"));
22         compareDatesByDateMethods(df, df.parse("02-03-2012"), df.parse("04-05-2012"));
23         compareDatesByDateMethods(df, df.parse("02-03-2012"), df.parse("01-02-2012"));
24  
25         //comparing dates in java using Calendar.before(), Calendar.after and Calendar.equals()
26         System.out.println("Comparing two Date in Java using Calendar's before, after and equals method");
27         compareDatesByCalendarMethods(df, df.parse("01-01-2012"), df.parse("01-01-2012"));
28         compareDatesByCalendarMethods(df, df.parse("02-03-2012"), df.parse("04-05-2012"));
29         compareDatesByCalendarMethods(df, df.parse("02-03-2012"), df.parse("01-02-2012"));
30  
31     }
32  
33     public static void compareDatesByCompareTo(DateFormat df, Date oldDate, Date newDate) {
34         //how to check if date1 is equal to date2
35         if (oldDate.compareTo(newDate) == 0) {
36             System.out.println(df.format(oldDate) + " and " + df.format(newDate) + " are equal to each other");
37         }
38  
39         //checking if date1 is less than date 2
40         if (oldDate.compareTo(newDate) < 0) {
41             System.out.println(df.format(oldDate) + " is less than " + df.format(newDate));
42         }
43  
44         //how to check if date1 is greater than date2 in java
45         if (oldDate.compareTo(newDate) > 0) {
46             System.out.println(df.format(oldDate) + " is greater than " + df.format(newDate));
47         }
48     }
49  
50     public static void compareDatesByDateMethods(DateFormat df, Date oldDate, Date newDate) {
51         //how to check if two dates are equals in java
52         if (oldDate.equals(newDate)) {
53             System.out.println(df.format(oldDate) + " and " + df.format(newDate) + " are equal to each other");
54         }
55  
56         //checking if date1 comes before date2
57         if (oldDate.before(newDate)) {
58             System.out.println(df.format(oldDate) + " comes before " + df.format(newDate));
59         }
60  
61         //checking if date1 comes after date2
62         if (oldDate.after(newDate)) {
63             System.out.println(df.format(oldDate) + " comes after " + df.format(newDate));
64         }
65     }
66  
67     public static void compareDatesByCalendarMethods(DateFormat df, Date oldDate, Date newDate) {
68  
69         //creating calendar instances for date comparision
70         Calendar oldCal = Calendar.getInstance();
71         Calendar newCal = Calendar.getInstance();
72  
73         oldCal.setTime(oldDate);
74         newCal.setTime(newDate);
75  
76         //how to check if two dates are equals in java using Calendar
77         if (oldCal.equals(newCal)) {
78             System.out.println(df.format(oldDate) + " and " + df.format(newDate) + " are equal to each other");
79         }
80  
81         //how to check if one date comes before another using Calendar
82         if (oldCal.before(newCal)) {
83             System.out.println(df.format(oldDate) + " comes before " + df.format(newDate));
84         }
85  
86         //how to check if one date comes after another using Calendar
87         if (oldCal.after(newCal)) {
88             System.out.println(df.format(oldDate) + " comes after " + df.format(newDate));
89         }
90     }
91}

[代码] 运行结果

01Comparing two Date in Java using CompareTo method
02 01-01-2012 and 01-01-2012 are equal to each other
03 02-03-2012 is less than 04-05-2012
04 02-03-2012 is greater than 01-02-2012
05Comparing two Date in Java using Date's before, after and equals method
06 01-01-2012 and 01-01-2012 are equal to each other
07 02-03-2012 comes before 04-05-2012
08 02-03-2012 comes after 01-02-2012
09Comparing two Date in Java using Calendar's before, after and equals method
10 01-01-2012 and 01-01-2012 are equal to each other
11 02-03-2012 comes before 04-05-2012
12 02-03-2012 comes after 01-02-2012
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值