3

I'd like to know the difference in days (excluding time).

These are my JDateChooser objects:

JDateChooser dateChooser_in = new JDateChooser();
JDateChooser dateChooser_out = new JDateChooser();

I tried to convert it to Date first, but I don't know how to so.

thomasfuchs
  • 5,386
  • 2
  • 18
  • 38
STEPHEN YAO
  • 105
  • 2
  • 2
  • 7

2 Answers2

1

JDateChooser has a getDate method which returns a java.util.Date. Once you have that, it's just a matter of using Java 8's 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());
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

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 TimeUnit documentation for more info.

MaxZoom
  • 7,619
  • 5
  • 28
  • 44
  • Why, why, why do people insist on doing this when there perfectly good date/time libraries available :( - Using `Calendar` to loop between the dates would be more preferable to this. This won't take into consideration the joys of century, millennium or other boundaries, day light savings, leap years/seconds and wrath of other issues with date/time ... – MadProgrammer Jul 20 '15 at 04:08
  • I do not like Joda library. – MaxZoom Jul 20 '15 at 04:09
  • Then you like incorrect data? Use `Calendar` if you don't like JodaTime, use Java 8's Time API if your don't like JodaTime, use just about any other time/date API, hell, use `Date4J`, but not millisecond maths! – MadProgrammer Jul 20 '15 at 04:10
  • Why not? Does not the library code will use milliseconds ? – MaxZoom Jul 20 '15 at 04:13
  • BUT, it doesn't take into consideration the rules governing how time is adjusted over the century and millennium boundaries or things like day light savings or leap years/seconds. It's generally an inaccurate measurement. There is not exactly 365 days in a year nor 24 hours in a day nor 60 seconds in a minute... – MadProgrammer Jul 20 '15 at 04:14
  • No, in most cases, this code will blow up in your face. Even if you're just trying to calculate the number of minutes between a few hours, you could cross a day light savings boundary... – MadProgrammer Jul 20 '15 at 04:41
  • Does not Java `Date` class takes care of the leap year problem? The daylight saving time does not make any difference when calculating days ( what is one hour to 24 hours) and considering `JDateChooser` gets date with zero time. – MaxZoom Jul 20 '15 at 04:59
  • When been formatted yes, but it's still just a container of milliseconds. As a general rule, millisecond calculations are a bad idea and get present way to much, when there are better alternatives available – MadProgrammer Jul 20 '15 at 05:13