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.

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class HashtableDemo {

public static void main(String args[]) throws AssertionError, ParseException {

DateFormat df = new SimpleDateFormat("dd-MM-yyyy");

//comparing date using compareTo method in Java
System.out.println("Comparing two Date in Java using CompareTo method");
compareDatesByCompareTo(df, df.parse("01-01-2012"), df.parse("01-01-2012"));
compareDatesByCompareTo(df, df.parse("02-03-2012"), df.parse("04-05-2012"));
compareDatesByCompareTo(df, df.parse("02-03-2012"), df.parse("01-02-2012"));

//comparing dates in java using Date.before, Date.after and Date.equals
System.out.println("Comparing two Date in Java using Date's before, after and equals method");
compareDatesByDateMethods(df, df.parse("01-01-2012"), df.parse("01-01-2012"));
compareDatesByDateMethods(df, df.parse("02-03-2012"), df.parse("04-05-2012"));
compareDatesByDateMethods(df, df.parse("02-03-2012"), df.parse("01-02-2012"));

//comparing dates in java using Calendar.before(), Calendar.after and Calendar.equals()
System.out.println("Comparing two Date in Java using Calendar's before, after and equals method");
compareDatesByCalendarMethods(df, df.parse("01-01-2012"), df.parse("01-01-2012"));
compareDatesByCalendarMethods(df, df.parse("02-03-2012"), df.parse("04-05-2012"));
compareDatesByCalendarMethods(df, df.parse("02-03-2012"), df.parse("01-02-2012"));

}

public static void compareDatesByCompareTo(DateFormat df, Date oldDate, Date newDate) {
//how to check if date1 is equal to date2
if (oldDate.compareTo(newDate) == 0) {
System.out.println(df.format(oldDate) + " and " + df.format(newDate) + " are equal to each other");
}

//checking if date1 is less than date 2
if (oldDate.compareTo(newDate) < 0) {
System.out.println(df.format(oldDate) + " is less than " + df.format(newDate));
}

//how to check if date1 is greater than date2 in java
if (oldDate.compareTo(newDate) > 0) {
System.out.println(df.format(oldDate) + " is greater than " + df.format(newDate));
}
}

public static void compareDatesByDateMethods(DateFormat df, Date oldDate, Date newDate) {
//how to check if two dates are equals in java
if (oldDate.equals(newDate)) {
System.out.println(df.format(oldDate) + " and " + df.format(newDate) + " are equal to each other");
}

//checking if date1 comes before date2
if (oldDate.before(newDate)) {
System.out.println(df.format(oldDate) + " comes before " + df.format(newDate));
}

//checking if date1 comes after date2
if (oldDate.after(newDate)) {
System.out.println(df.format(oldDate) + " comes after " + df.format(newDate));
}
}

public static void compareDatesByCalendarMethods(DateFormat df, Date oldDate, Date newDate) {

//creating calendar instances for date comparision
Calendar oldCal = Calendar.getInstance();
Calendar newCal = Calendar.getInstance();

oldCal.setTime(oldDate);
newCal.setTime(newDate);

//how to check if two dates are equals in java using Calendar
if (oldCal.equals(newCal)) {
System.out.println(df.format(oldDate) + " and " + df.format(newDate) + " are equal to each other");
}

//how to check if one date comes before another using Calendar
if (oldCal.before(newCal)) {
System.out.println(df.format(oldDate) + " comes before " + df.format(newDate));
}

//how to check if one date comes after another using Calendar
if (oldCal.after(newCal)) {
System.out.println(df.format(oldDate) + " comes after " + df.format(newDate));
}
}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值