java.util.Calendar after()方法
java.util.Calendar.after(Object when) 如果此Calendar表示的时间晚于when对象表示的时间,则此方法返回true 。如果不是这种情况,则返回false。
1 语法
public boolean after(Object when)
2 参数
when:将要比较的时间对象。
3 返回值
如果此日历表示的时间晚于when对象表示的时间,则为true;否则为true。否则为false。
4 示例1
package com.yiidian;
/**
* 一点教程网: http://www.yiidian.com
*/
/**
* java.util.Calendar after()方法的例子
*/
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.
5 示例2
package com.yiidian;
/**
* 一点教程网: http://www.yiidian.com
*/
/**
* java.util.Calendar after()方法的例子
*/
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.
6 示例3
package com.yiidian;
/**
* 一点教程网: http://www.yiidian.com
*/
/**
* java.util.Calendar after()方法的例子
*/
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.
7 示例4
package com.yiidian;
/**
* 一点教程网: http://www.yiidian.com
*/
/**
* java.util.Calendar after()方法的例子
*/
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.
热门文章
优秀文章