如何在Java中检查Date是否在某个范围内?
一个Java示例,用于检查提供的日期是否在当前日期前后3个月的范围内。 这个想法非常简单,只需使用类来回溯月份以创建“日期范围”,然后使用Date.before()和Date.after()来检查Date是否在该范围内。
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(" currentDate : " + 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
标签: 标签:
翻译自:
上一篇:
5款热门的远程控制软件,让你事半功倍