2 Answers. Edit: To get the months difference, you can use Calender class. Otherwise, use JodaTime library (linked at bottom of answer). Calendar startCalendar = new GregorianCalendar (); startCalendar.setTime (startDate); Calendar endCalendar = new GregorianCalendar (); endCalendar.setTime (endDate); int diffYear = endCalendar.get (Calendar
For this type of problem, it is best to use the new Java Time API that was introduced in Java 8.. We have 2 Strings that represent a year and a month. Therefore, we will parse each String into a YearMonth object using a custom DateTimeFormatter.I think the easy solution is to convert to LocalDate: LocalDate validFrom = LocalDate.parse ("2017-01-01 00:00:00", DateTimeFormatter.ofPattern ("uuuu-MM-dd HH:mm:ss")); The LocalDate class accepts parsing with a pattern that includes hours, minutes and seconds, it just ignores those values from the string. Do the same with validUntil and the The user could change the selected month & year by clicking the next/previous month buttons, selecting a month from the month popup menu, or selecting a different year (using the year spinner): Edit: added example with adapted versions of JDatePicker classes Wo79UNX.