当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Calendar after()用法及代码示例


如果此 Calendar 表示的时间晚于 when 对象表示的时间,则此方法返回 true。如果不是这种情况,则返回 false。

用法

Public boolean after (Object when)

参数

When - 即将进行比较的时间对象。

返回

如果此 Calendar 表示的时间晚于 when 对象表示的时间,则为真;否则为假。

例子1

import java.util.*;
public class CalendarafterExample1{
   public static void main(String[] args) {   
      // create calendar objects.
       Calendar cal = (Calendar) Calendar.getInstance();
      Calendar future = (Calendar) Calendar.getInstance();
      // print the current date
      System.out.println("Current date:" + cal.getTime());
      // change year in future calendar
      future.set(Calendar.YEAR, 2015);
      System.out.println("Year is " + future.get(Calendar.YEAR));
      // check if calendar date is after current date
      Date time = future.getTime();      
      if (!future.after(cal)) {
         System.out.println("Date " + time + " is before current date.");
      }
   }
}

输出:

Current date:Sun Jul 29 07:50:47 PDT 2018
Year is 2015
Date Wed Jul 29 07:50:47 PDT 2015 is before current date.

例子2

import java.util.Calendar;
import java.util.Date;
public class  CalendarafterExample2 {   
public static void main(String[] args) {   
      // create calendar objects.
       Calendar cal = (Calendar) Calendar.getInstance();
      Calendar cal2 = (Calendar) Calendar.getInstance();
      // print the current date
      System.out.println("Current date:" + cal.getTime());
      // change year in future calendar
      cal2.set(Calendar.MONTH, 10);
      System.out.println("Year is " + cal2.get(Calendar.MONTH));
      // check if calendar date is after current date
      Date time = cal2.getTime();     
      if (cal2.after(cal)) {
         System.out.println("Date " + time + " is after current date.");
      }
   }
}

输出:

Current date:Sun Jul 29 07:52:17 PDT 2018
Year is 10
Date Thu Nov 29 07:52:17 PST 2018 is after current date.

例子3

import java.util.Calendar;
import java.util.Date;
public class CalendarafterExample3 {    
public static void main(String[] args) {  
      // create calendar objects.
       Calendar cal = (Calendar) Calendar.getInstance();
      Calendar cal3 = (Calendar) Calendar.getInstance();
      // print the current date
      System.out.println("Current date:" + cal.getTime());
      // change year in future calendar
      cal3.set(Calendar.DAY_OF_MONTH, 30);
      System.out.println("Year is " + cal3.get(Calendar.DAY_OF_MONTH));
      // check if calendar date is after current date
      Date time = cal3.getTime();     
      if (cal3.after(cal)) {
         System.out.println("Date " + time + " is after current date.");
      }
   }
}

输出:

Current date:Sun Jul 29 07:53:23 PDT 2018
Year is 30
Date Mon Jul 30 07:53:23 PDT 2018 is after current date.

示例 4

import java.util.Calendar;
import java.util.Date;
public class CalendarafterExample4 {    
public static void main(String[] args) {   
      // create calendar objects.
      Calendar cal = (Calendar) Calendar.getInstance();
      Calendar cal3 = (Calendar) Calendar.getInstance();
      // print the current date
      System.out.println("Current date:" + cal.getTime());
      // change year in future calendar
      cal3.set(Calendar.HOUR, 30);
      System.out.println("Now date is  " + cal3.getTime());
       cal3.set(Calendar.MINUTE, 60);
      System.out.println("Now date is  " + cal3.getTime());     
       cal3.set(Calendar.SECOND, 30);
      System.out.println("Now date is  " + cal3.getTime());    
      // check if calendar date is after current date
      Date time = cal3.getTime();      
      if (cal3.after(cal)) {
         System.out.println("Date " + time + " is after current date.");
      }
   }
}

输出:

Output
Current date:Sun Jul 29 07:54:14 PDT 2018
Now date is  Mon Jul 30 06:54:14 PDT 2018
Now date is  Mon Jul 30 07:00:14 PDT 2018
Now date is  Mon Jul 30 07:00:30 PDT 2018
Date Mon Jul 30 07:00:30 PDT 2018 is after current date.



相关用法


注:本文由纯净天空筛选整理自 Java Calendar after() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。