How to get the difference in days between two JDateChooser objects
2 answers
JDateChooser
has a method getDate
that returns a java.util.Date
. Once you've done that, it's just a matter of using the Java 8 Time API or JodaTime to calculate the difference
Java 8
LocalDateTime from = LocalDateTime.ofInstant(dateChooser_in.getDate().toInstant(), ZoneId.systemDefault());
LocalDateTime to = LocalDateTime.ofInstant(dateChooser_out.getDate().toInstant(), ZoneId.systemDefault());
Duration d = Duration.between(from, to);
System.out.println(d.toDays());
Joda time
LocalDate from = LocalDate.fromDateFields(dateChooser_in.getDate());
LocalDate to = LocalDate.fromDateFields(dateChooser_out.getDate());
System.out.println(Days.daysBetween(from, to).getDays());
+1
source to share
If you have two dates, the number of days can be calculated as:
Date date1 = myFormat.parse(inputString1);
Date date2 = myFormat.parse(inputString2);
long diff = date2.getTime() - date1.getTime();
System.out.println ("Number of days: " + TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS));
See the TimeUnit documentation for more information .
0
source to share