Java tutorial
/* * The MIT License (MIT) * Copyright (c) 2015 "Yukthi Techsoft Pvt. Ltd." (http://yukthi-tech.co.in) * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package com.yukthi.validators; import java.util.Calendar; import java.util.Date; import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; import org.apache.commons.lang3.time.DateUtils; import com.yukthi.validation.annotations.FutureOrToday; /** * Validator - to validate the target date either future date or today * @author akiran */ public class FutureOrTodayValidator implements ConstraintValidator<FutureOrToday, Date> { /* (non-Javadoc) * @see javax.validation.ConstraintValidator#initialize(java.lang.annotation.Annotation) */ @Override public void initialize(FutureOrToday matchWith) { } /* (non-Javadoc) * @see javax.validation.ConstraintValidator#isValid(java.lang.Object, javax.validation.ConstraintValidatorContext) */ @Override public boolean isValid(Date targetDate, ConstraintValidatorContext context) { //if target date is not present, ignore validator if (targetDate == null) { return true; } //compare target date with today Date today = DateUtils.truncate(new Date(), Calendar.DATE); targetDate = DateUtils.truncate(targetDate, Calendar.DATE); return (targetDate.compareTo(today) >= 0); } }