java.util.Calendar before()方法
java.util.Calendar.before(Object when) 是java.util包的Calendar类中的方法。如果此Calendar表示的时间早于when对象表示的时间,则该方法返回true。如果不是这种情况,则返回false。
1 语法
public boolean before(Object when)
2 参数
when:when是要比较的对象。
3 返回值
此方法返回true或false值
4 示例1
package com.yiidian;
/**
* 一点教程网: http://www.yiidian.com
*/
/**
* java.util.Calendar before()方法的例子
*/
import java.util.Calendar;
import java.util.Date;
public class CalendarbeforeExample1 {
public static void main(String[] args) {
// create calendar objects.
Calendar cal = (Calendar) Calendar.getInstance();
Calendar past = (Calendar) Calendar.getInstance();
// print the current date
System.out.println("Current date: " + cal.getTime());
// change year in future calendar
past.set(Calendar.YEAR, 2015);
System.out.println("Year is " + past.get(Calendar.YEAR));
// check if calendar date is after current date
Date time = past.getTime();
if (past.before(cal)) {
System.out.println("Date " + time + " is before current date.");
}
}
}
输出结果为:
Current date: Mon Jul 30 04:08:30 PDT 2018
Year is 2015
Date Thu Jul 30 04:08:30 PDT 2015 is before current date.
5 示例2
package com.yiidian;
/**
* 一点教程网: http://www.yiidian.com
*/
/**
* java.util.Calendar before()方法的例子
*/
import java.util.Calendar;
import java.util.Date;
public class CalendarbeforeExample2 {
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, 4);
System.out.println("Year is " + cal2.get(Calendar.MONTH));
// check if calendar date is after current date
Date time = cal2.getTime();
if (cal2.before(cal)) {
System.out.println("Date " + time + " is before current date.");
}
}
}
输出结果为:
Current date: Mon Jul 30 04:13:04 PDT 2018
Year is 4
Date Wed May 30 04:13:04 PDT 2018 is before current date.
6 示例3
package com.yiidian;
/**
* 一点教程网: http://www.yiidian.com
*/
/**
* java.util.Calendar before()方法的例子
*/
import java.util.Calendar;
import java.util.Date;
public class CalendarbeforeExample3 {
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, 23);
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.before(cal)) {
System.out.println("Date " + time + " is before current date.");
}
}
}
输出结果为:
Current date: Mon Jul 30 06:19:04 PDT 2018
Year is 23
Date Mon Jul 23 06:19:04 PDT 2018 is before current date.
7 示例4
package com.yiidian;
/**
* 一点教程网: http://www.yiidian.com
*/
/**
* java.util.Calendar before()方法的例子
*/
import java.util.Calendar;
import java.util.Date;
public class CalendarbeforeExample4 {
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.before(cal)) {
System.out.println("Date " + time + " is after current date.");
}
}
}
输出结果为:
Current date: Mon Jul 30 06:23:37 PDT 2018
Now date is Tue Jul 31 06:23:37 PDT 2018
Now date is Tue Jul 31 07:00:37 PDT 2018
Now date is Tue Jul 31 07:00:30 PDT 2018
热门文章
优秀文章