在 Java 中比较日期的多种方式有哪些?
使用 LocalDate 类
java.time.LocalDate 类表示本地日期,即不含时区的日期,你可以使用此对象,而不是 Date。此类提供多种方法,如 isBefore()、isAfter() 和 isEqual(),以比较两个日期 -
示例
import java.time.LocalDate; public class Sample { public static void main(String args[]) { LocalDate date1 = LocalDate.of(2007, 11, 25); LocalDate date2 = LocalDate.of(1999, 9, 12); Boolean bool1 = date1.isAfter(date2); Boolean bool2 = date1.isBefore(date2); Boolean bool3 = date1.isEqual(date2); if(bool1){ System.out.println(date1+" is after "+date2); }else if(bool2){ System.out.println(date1+" is before "+date2); }else if(bool3){ System.out.println(date1+" is equla to "+date2); } } }
输出
2007-11-25 is after 1999-09-12
使用 Date 类
java.util.Date 类表示特定时间。此类提供多种方法,如 before()、after() 和 equals(),以比较两个日期 -
示例
import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.util.Date; public class Sample { public static void main(String args[]) throws ParseException { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-dd-MM"); //Parsing the given String to Date object Date date1 = formatter.parse("2007-11-25"); Date date2 = formatter.parse("1999-9-12"); Boolean bool1 = date1.after(date2); Boolean bool2 = date1.before(date2); Boolean bool3 = date1.equals(date2); if(bool1){ System.out.println(date1+" is after "+date2); }else if(bool2){ System.out.println(date1+" is before "+date2); }else if(bool3){ System.out.println(date1+" is equals to "+date2); } } }
输出
Sun Jan 11 00:00:00 IST 2009 is after Thu Dec 09 00:00:00 IST 1999
使用 Calendar 类
与 Date 类类似,Calendar 类提供多种方法,如 before()、after() 和 equals(),以比较两个日期 -
示例
import java.util.Calendar; public class Sample { public static void main(String args[]) { Calendar obj1 = Calendar.getInstance(); obj1.set(2007, 11, 25); Calendar obj2 = Calendar.getInstance(); obj2.set(1999, 9, 15); Boolean bool1 = obj1.after(obj2); Boolean bool2 = obj1.before(obj2); Boolean bool3 = obj1.equals(obj2); if(bool1){ System.out.println(obj1+" is after "+obj2); }else if(bool2){ System.out.println(obj1+" is before "+obj2); }else if(bool3){ System.out.println(obj1+" is equla to "+obj2); } } }
输出
java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=7,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2007,MONTH=11,WEEK_OF_YEAR=45,WEEK_OF_MONTH=1,DAY_OF_MONTH=25,DAY_OF_YEAR=312,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=9,HOUR_OF_DAY=21,MINUTE=33,SECOND=47,MILLISECOND=366,ZONE_OFFSET=19800000,DST_OFFSET=0] is after java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=7,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=1999,MONTH=9,WEEK_OF_YEAR=45,WEEK_OF_MONTH=1,DAY_OF_MONTH=15,DAY_OF_YEAR=312,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=9,HOUR_OF_DAY=21,MINUTE=33,SECOND=47,MILLISECOND=392,ZONE_OFFSET=19800000,DST_OFFSET=0]