tl;dr
Period.between(
LocalDate.of( 2016 , Month.JANUARY , 22 ) ,
LocalDate.of( 2016 , Month.MARCH , 30 )
).isNegative()
Details
Your Question is not clear, but this might help point you in the right direction.
Avoid the troublesome old date-time classes such as Date
that are now legacy, supplanted by the java.time classes.
LocalDate
The LocalDate
class represents a date-only value without time-of-day and without time zone.
date1 = 22/01/2016
date2 = 30/03/2016
LocalDate start = LocalDate.of( 2016 , Month.JANUARY , 22 ) ;
LocalDate stop = LocalDate.of( 2016 , Month.MARCH , 30 ) ;
Period
Calculate the number of days, months, and years elapsed as a Period
.
Period period = Period.between( start , stop ) ;
Test if the stop comes before the start, meaning the elapsed time is negative, going backwards along the timeline.
Boolean isTimeGoingBackwards = period.isNegative() ;
Months
Your comments mention getting a number of months. Understand multiple ways to count months:
- Count days elapsed, divided by 30 as the length of a generic month.
- Count of calendar months completely covered
- Count of calendar months touched by the span of time
Java offers at least some of these. Read the documentation to decide which meets your needs. If going backwards in time, the resulting number is negative.
ChronoUnit.MONTHS
long months = ChronoUnit.MONTHS.between( start , stop ) ;
Period.toTotalMonths
int months = Period.between( start , stop ).toTotalMonths() ;
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.
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.