Calendar.after()方法返回此Calendar的时间是否晚于指定Object表示的时间。
首先,让我们设置一个在当前日期(未来日期)之后的日期
Calendar afterDate = Calendar.getInstance(); afterDate.set(Calendar.YEAR, 2025); afterDate.set(Calendar.MONTH, 05); afterDate.set(Calendar.DATE, 30);
这是我们的约会时间,2018年11月22日
Calendar currentDate = Calendar.getInstance();
现在,使用after()
方法比较两个日期,如以下示例所示
import java.util.Calendar; public class Demo { public static void main(String[] args) { Calendar afterDate = Calendar.getInstance(); afterDate.set(Calendar.YEAR, 2025); afterDate.set(Calendar.MONTH, 05); afterDate.set(Calendar.DATE, 30); java.util.Date dt = afterDate.getTime(); System.out.println("Future Date = "+dt); Calendar currentDate = Calendar.getInstance(); System.out.println("Current date = " + (currentDate.get(Calendar.MONTH) + 1) + "-" + currentDate.get(Calendar.DATE) + "-" + currentDate.get(Calendar.YEAR)); System.out.println("Date "+dt+ " is after current date? = " + afterDate.after(currentDate)); } }
输出结果
Future Date = Mon Jun 30 12:36:27 UTC 2025 Current date = 11-22-2018 Date Mon Jun 30 12:36:27 UTC 2025 is after current date? = true