1

This is my codes but this is not working.

    Start.getDate();
    End.getDate();
    int diffInDays = (int)( (Start.getDate() - End.getDate()) / (1000 * 60 * 60 * 24));
    System.out.println(diffInDays);

So how can i make a program that the user can view the difference between 2 JDateChooser.

Xyryle Buen
  • 43
  • 2
  • 12

2 Answers2

5

Date/Time calculations are a complex subject which are typically is best solved using a dedicated library to solve.

Java 8

Instant start = Start.getDate().toInstance();
Instant end = End.getDate().getInstance();

Duration duration = Duration.between(start, end);

Joda-Time

If you can't use Java 8, then you should use Joda-Time instead

DateTime start = new DateTime(Start.geDate().getTime());
DateTime end = new DateTime(End.geDate().getTime());

// You might need to use end, start instead, depending on which
// is later
Duration duration = new Duration(start, end);
Period period = duration.toPeriod();

You can have a look at this answer to see how you might format the value

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • hi Mad how can i use Java 8? sorry i was a beginner. – Xyryle Buen Apr 17 '15 at 03:05
  • is it a library for netbeans or what? – Xyryle Buen Apr 17 '15 at 03:07
  • Java 1.8 is the current version of Java. All IDEs can take advantage of it. – MadProgrammer Apr 17 '15 at 03:45
  • Mad, please help me. i just want to clear the field or the value of JDateChooser do u have any suggestions please. – Xyryle Buen Apr 17 '15 at 03:58
  • What happens when you pass the field null? – MadProgrammer Apr 17 '15 at 04:48
  • In my program the user can click on the Table to view the information of each row. and each row is connected to the database so the Start(JDateChooser) & End(JDateChooser) has a value on the database. the problem is when the user clicked on the row which has a value for Start & End the value successfully showed on JDateChoosers based on the database BUT when the user click again on another row which is the row dont have a value for Start & End the JDatechoosers is not making a NULL or ""(Empty)? it stil have a value. – Xyryle Buen Apr 17 '15 at 04:56
  • Okay, this isn't related to your question, I'd suggest creating a new question, in which you can provide a runnable sample which demonstrates your problem – MadProgrammer Apr 17 '15 at 05:01
  • Runnable Sample? how can i make it? – Xyryle Buen Apr 17 '15 at 05:03
  • You wrote the code. It should be contained to a single class (but you can use inner classes) which focuses on the demonstrating the problem you're having. Other people should be able to copy the code and paste it in their ide, compile and run it – MadProgrammer Apr 17 '15 at 05:23
  • i used the Joda Time . what if i change the start variable to current date can you give me an example code? – Xyryle Buen Apr 21 '15 at 01:13
  • 1
    `new DateTime()` will give the current date/time – MadProgrammer Apr 21 '15 at 01:21
  • how about how can i print it only the day difference? – Xyryle Buen Apr 21 '15 at 01:30
  • 1
    [`Duraton#standardDays`](http://joda-time.sourceforge.net/apidocs/org/joda/time/Duration.html#standardDays(long)) – MadProgrammer Apr 21 '15 at 01:34
  • @XyryleBuen Yes, apparently ;) – MadProgrammer Apr 21 '15 at 02:11
  • JodaTime and the Time API can seem confusing to start with, best thing is to play around with the various classes and methods and read the docs and tutorials, I find new stuff all the time ;) – MadProgrammer Apr 21 '15 at 02:12
  • Thanks to this forum because it helps me so much. if there's a way to donate in this website i do it. – Xyryle Buen Apr 21 '15 at 02:25
2

Try getting the times in milliseconds and comparing them:

Date startDate = Start.getDate();
Date endDate = End.getDate();

// In milliseconds
long difference = endDate.getTime() - startDate.getTime();

int diffInDays = (int)(difference / 1000 / 60 / 60 / 24);

Before you were subtracting the Date objects, which aren't numbers. You want to compare their times, which are numbers.

To check if the JDateChooser is null, just do:

if (Start == null) {
    // code
}

or

if (Start != null) {
    // code
}

Disclaimer:

You should NEVER subtract two milliseconds from each other,there are just to many rules which aren't taking into account (leap years, leap seconds, century boundaries, daylight savings) to make it even remotely accurate. You should either use Java 8's Time API or JodaTi e (oh and days aren't always 24 hours long). -MadProgrammer.

This is a quick and dirty solution, but there are a lot of weak edge cases. Take caution.

Community
  • 1
  • 1
Anubian Noob
  • 13,426
  • 6
  • 53
  • 75
  • btw, how can i know if the 2 jdatechooser is null? can you give me another code? – Xyryle Buen Apr 17 '15 at 01:55
  • Is that what you're looking for? – Anubian Noob Apr 17 '15 at 02:01
  • 1
    You should NEVER subtract two milliseconds from each other,there are just to many rules which aren't taking into account (leap years, leap seconds, century boundaries, daylight savings) to make it even remotely accurate. You should either use Java 8's Time API or JodaTi e (oh and days aren't always 24 hours long) – MadProgrammer Apr 17 '15 at 02:06
  • @MadProgrammer I mean this is a quick and effective solution, and I'm pretty sure it's good enough for the scope of the poster. I'll add that as a disclaimer though. – Anubian Noob Apr 17 '15 at 02:07
  • It's neither quick or effective, that's the problem - it's a bad solution to a complex problem and people should simply stop suggesting it - sorry – MadProgrammer Apr 17 '15 at 02:08
  • 1
    And `Duration.between(Temporal, Temporal)` isn't (quick)? – MadProgrammer Apr 17 '15 at 02:19
  • Easy guys. my problem is now solved. i research about how to calculate 2 dates and i always see in all forum is the Joda maybe later i should study about that. My problem right now is how can i set the date of jDateCHooser to null or "0000-00-00" – Xyryle Buen Apr 17 '15 at 02:45
  • Ask another question or look it up – Anubian Noob Apr 17 '15 at 02:48
  • sorry i changed the accepted answer because of the Joda Time it helps me a lot. – Xyryle Buen Apr 21 '15 at 01:58