3

I'm currently trying to improve an aspect of a project of mine.

Users are allowed to do a specific task, but they must book a date in order to do it.

I'm trying to add some more realistic validation onto my date, so that the tasks can't be booked a year in advance, and only a few months.

Currently I'm only checking the year of the input and comparing it to the current year, so if they try to assign themselves a task on 31st of December, they will not be able to because any date they enter will roll over to the next year, and my validation prevents this.

How can I make it so it will check the amount of months, rather than the current year?

I am able to do this for the current year, I just get stuck when the year comes to december and the months roll into January again.

Edit: Those looking for a way to fix this, go here: Calculating the difference between two Java date instances

Community
  • 1
  • 1
kbz
  • 984
  • 2
  • 12
  • 30

6 Answers6

2

Because the lengths of months are different, I would test the number of days. Here's a couple of utility methods that get the job done in one line:

// Tests if the end date is within so many days of the start date 
public static boolean isWithinRange(int days,  Date end, Date start) {
    return TimeUnit.DAYS.convert(end.getTime() - start.getTime(), TimeUnit.MILLISECONDS) < days;
}

// Tests if the specified date is within so many days of today 
public static boolean isWithinRange(int days, Date end) {
    return isWithinRange(days, end, new Date());
}

Here I've used the TimeUnit class to do the calculation for me.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
1

you can use your own method. Something like this

public boolean isLaterDay(Date date, Date reference) {
    if (date.getYear () > reference.getYear ()) return true;
    if (date.getYear () < reference.getYear ()) return false;
    return (date.getMonth() > reference.getMonth());
}
Kanwaljeet Singh
  • 1,215
  • 4
  • 15
  • 25
1

Another way of doing this would be as follows.

boolean validDate(Calendar inputDate)
{
    Calendar validationDate = Calendar.getInstance().add(Calendar.MONTH, numOfMonths);
    return inputDate.before(validationDate);
}
Izaaz Yunus
  • 2,828
  • 1
  • 19
  • 28
1

You can do something like this to validate the time

private static final int MAX_MONTHS_IN_ADVANCE = 3;

public boolean isValidDate(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.add(Calendar.MONTH, MAX_MONTHS_IN_ADVANCE);
    return date.before(calendar.getTime());
}
SamYonnou
  • 2,068
  • 1
  • 19
  • 23
1

Using the Joda-Time library:

If ( dateTimeInQuestion.isBefore( DateTime.now().plusMonths(3) )
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

java.time

The modern way to do this work is with the java.time classes. These classes supplant the troublesome old legacy date-time classes.

LocalDate

The LocalDate class represents a date-only value without time-of-day and without time zone.

A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.

ZoneId z = ZoneId.of( “America/Montreal” );
LocalDate today = LocalDate.now( z );

Construct the date desired by the user.

LocalDate ld = LocalDate.of( 2016 , 12 , 31 );

Determine the boundaries, say six months ago and six months from now.

LocalDate past = today.minusMonths( 6 );
LocalDate future = today.plusMonths( 6 );

You can compare LocalDate objects with isBefore, isAfter, equals, and compareTo.

Let's test by asking if the user's date is equal to or later than the before boundary (in other words, not before) AND the user's date is before the future boundary. This comparison uses the Half-Open approach commonly used with date-time work. The beginning is inclusive while the ending is exclusive.

Boolean validDate = ( ( ! ld.isBefore( past) ) && ( ld.isBefore( future) ) );

Interval

If you often work with the spans of time, consider using the Interval class found in the ThreeTen-Extra project that adds onto the java.time classes. That class has handy methods such as contains, abuts, overlaps, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154