-2

How to validate End Date should not be more than 60 days from start date in Java

Example 9/1/2018 is start date should not be able to enter End Date less than 60 days i.e. 11/1/2018

rohith
  • 11
  • 1

1 Answers1

2

tl;dr

stop.isBefore( 
    start.plusDays( 60 ) 
) 

Details

Use LocalDate for a date-only value without time-of-day and without time zone.

LocalDate start = LocalDate.of( 2018 , 1 , 23 ) ;
LocalDate stop = … ;

Compare with isBefore, isAfter, and such methods.

Boolean isNotTooEarly = stop.isAfter( start ) ; 

int limit = 60 ; // 60 days maximum. 
Boolean isNotTooLate = stop.isBefore( start.plusDays( limit ) ) ;

Boolean isValid = ( isNotTooEarly && isNotTooLate ) ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

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