三种方法分别是:
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) 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]代码
01 | import java.text.DateFormat; |
02 | import java.text.ParseException; |
03 | import java.text.SimpleDateFormat; |
04 | import java.util.Calendar; |
05 | import java.util.Date; |
06 | |
07 | public 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 | compareDatesByDateMethod "01-01-2012" ), df.parse( "01-01-2012" )); |
22 | compareDatesByDateMethod "02-03-2012" ), df.parse( "04-05-2012" )); |
23 | compareDatesByDateMethod "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 | compareDatesByCalendarMe "01-01-2012" ), df.parse( "01-01-2012" )); |
28 | compareDatesByCalendarMe "02-03-2012" ), df.parse( "04-05-2012" )); |
29 | compareDatesByCalendarMe "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 compareDatesByDateMethod |
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 compareDatesByCalendarMe |
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 | } |
[代码] 运行结果
01 | Comparing 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 |
05 | Comparing 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 |
09 | Comparing 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 |