Java日期比较(OSChina.NET)

 1 import java.text.DateFormat;

 2 import java.text.ParseException;

 3 import java.text.SimpleDateFormat;

 4 import java.util.Calendar;

 5 import java.util.Date;

 6 

 7 public class HashtableDemo {

 8 

 9     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 }

 

你可能感兴趣的:(java)