提供された日付が現在の日付の前後3ヵ月の範囲内にあるかどうかを調べるJavaの例。アイデアは非常にシンプルです。http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html[Calendar]クラスを使用して月を前後にロールして、 Date.before()



Date.after() `を使って日付が範囲内にあるかどうかをチェックします。

if (dateToValidate.before(currentDateAfter3Months.getTime())
        && dateToValidate.after(currentDateBefore3Months.getTime())) {

完全な例を参照してください。

package com.mkyong.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateValidator {

    public boolean isThisDateWithin3MonthsRange(String dateToValidate,
            String dateFromat) {

        SimpleDateFormat sdf = new SimpleDateFormat(dateFromat);
        sdf.setLenient(false);
        try {

           //if not valid, it will throw ParseException
            Date date = sdf.parse(dateToValidate);

           //current date after 3 months
            Calendar currentDateAfter3Months = Calendar.getInstance();
            currentDateAfter3Months.add(Calendar.MONTH, 3);

           //current date before 3 months
            Calendar currentDateBefore3Months = Calendar.getInstance();
            currentDateBefore3Months.add(Calendar.MONTH, -3);

           /** ** ** ** ** ** ** ** ** ** ** ** ** ** **  verbose ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** /            System.out.println("\n\ncurrentDate : "
                    + Calendar.getInstance().getTime());
            System.out.println("currentDateAfter3Months : "
                    + currentDateAfter3Months.getTime());
            System.out.println("currentDateBefore3Months : "
                    + currentDateBefore3Months.getTime());
            System.out.println("dateToValidate : " + dateToValidate);
           /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** /
            if (date.before(currentDateAfter3Months.getTime())
                    && date.after(currentDateBefore3Months.getTime())) {

               //ok everything is fine, date in range
                return true;

            } else {

                return false;

            }

        } catch (ParseException e) {

            e.printStackTrace();
            return false;
        }

    }

}

上記のコードを証明するための単純な単体テストが正しく動作しています。単体テストの下で実行すると、すべてのケースが渡されます。

package com.mkyong.test;

import org.junit.** ;
import com.mkyong.date.DateValidator;
import static org.junit.Assert.** ;

public class DateValidatorRangeTest {

    private DateValidator dateValidator;

    @Before
    public void init() {
        dateValidator = new DateValidator();
    }

    @Test
    public void testDateWithinRange__1() {
        assertTrue(dateValidator.isThisDateWithin3MonthsRange("31/01/2012", "dd/MM/yyyy"));
    }

    @Test
    public void testDateWithinRange__2() {
        assertFalse(dateValidator.isThisDateWithin3MonthsRange("31/01/2011", "dd/MM/yyyy"));
    }

    @Test
    public void testDateWithinRange__3() {
        assertTrue(dateValidator.isThisDateWithin3MonthsRange("20/02/2012", "dd/MM/yyyy"));
    }

    @Test
    public void testDateWithinRange__4() {
        assertFalse(dateValidator.isThisDateWithin3MonthsRange("21/05/2012", "dd/MM/yyyy"));
    }

}

すべてのケースは、コンソール上の追加情報に続いて渡され、出力されます。

currentDate : Mon Feb 20 13:36:59 SGT 2012
currentDateAfter3Months : Sun May 20 13:36:59 SGT 2012
currentDateBefore3Months : Sun Nov 20 13:36:59 SGT 2011
dateToValidate : 31/01/2012


currentDate : Mon Feb 20 13:36:59 SGT 2012
currentDateAfter3Months : Sun May 20 13:36:59 SGT 2012
currentDateBefore3Months : Sun Nov 20 13:36:59 SGT 2011
dateToValidate : 31/01/2011


currentDate : Mon Feb 20 13:36:59 SGT 2012
currentDateAfter3Months : Sun May 20 13:36:59 SGT 2012
currentDateBefore3Months : Sun Nov 20 13:36:59 SGT 2011
dateToValidate : 20/02/2012


currentDate : Mon Feb 20 13:36:59 SGT 2012
currentDateAfter3Months : Sun May 20 13:36:59 SGT 2012
currentDateBefore3Months : Sun Nov 20 13:36:59 SGT 2011
dateToValidate : 21/05/2012

リンク://タグ/日付/[日付]リンク://タグ/java/[java]