Java-util-calendar-before

提供:Dev Guides
移動先:案内検索

Java.util.Calendar.before()メソッド

説明

  • java.util.Calendar.before()*メソッドは、このカレンダーの時刻が、指定されたObject *(when)*で表される時刻より前かどうかを返します。

宣言

以下は* java.util.Calendar.before()*メソッドの宣言です

public boolean before(Object when)

パラメーター

  • when -比較される時間のオブジェクト。

戻り値

このCalendarが表す時刻がwhenオブジェクトが表す時刻より前の場合はtrue。それ以外の場合はfalse。

例外

NA

次の例は、java.util.Calendar.before()メソッドの使用方法を示しています。

package com.finddevguides;

import java.util.Calendar;

public class CalendarDemo {
   public static void main(String[] args) {

     //create calendar objects.
      Calendar cal = Calendar.getInstance();
      Calendar past = Calendar.getInstance();

     //print the current date
      System.out.println("Current date: " + cal.getTime());

     //change year in past calendar
      past.set(Calendar.YEAR, 2006);
      System.out.println("Year is " + past.get(Calendar.YEAR));

     //check if calendar date is before current date
      System.out.println("Before current date:" + cal.before(past));
   }
}

上記のプログラムをコンパイルして実行すると、次の結果が生成されます-

Current date: Sat May 05 15:59:37 EEST 2012
Year is 2006
Before current date:false