I would like to validate the date input of the user based on a condition. The condition is new date entered by user should be greater than 32 days. How to do the same?. I have tried the following but it is not working.
final Date getuserDate = validate.date();
logger.debug("UserDate is" + getuserDate);
DateTime currentExpdelDate = new DateTime(getuserDate);
DateTime currentDate = new DateTime();
DateTime dtPlus = currentDate.plusDays(32);
long millisec = dtPlus.getMillis() - currentExpdelDate.getMillis();
if (millisec > 0) {
long diffDays = millisec / (24 * 60 * 60 * 1000);
if (diffDays < 32) {
System.out.println("Date should be greater than 32 days")
}
}
The universal type DateTime
is not suitable for a date-only task. You should therefore choose the type LocalDate
in the Joda-Time-library.
Then you can use this constructor . Note that you need to specify the timezone because the current date is not the same date all around in the world at the same time.
java.util.Date input = ...;
DateTimeZone zone = DateTimeZone.forID(...);
LocalDate userDate = new LocalDate(input, zone);
boolean exclude = new LocalDate(zone).plusDays(32).isAfter(userDate);
if (exclude) {
System.out.println("Date should be greater than 32 days in the future.");
}
如果您已经在使用Java 8 ,则可以使用plusDays()
和isBefore()
方法使用其新的LocalDate
类。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.