Javaで日付が特定の範囲内にあるかどうかを確認するにはどうすればよいですか?
指定された日付が現在の日付の前後3か月以内にあるかどうかを確認するJavaの例。 考え方は非常に単純です。Calendarクラスを使用して月を前後にロールバックして「日付範囲」を作成し、Date.before()
とDate.after()
を使用して日付が範囲内にあるかどうかを確認するだけです。範囲。
if (dateToValidate.before(currentDateAfter3Months.getTime()) && dateToValidate.after(currentDateBefore3Months.getTime())) {
完全な例を参照してください。
package com.example.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.example.test; import org.junit.*; import com.example.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