0

after testing for hours and looking through the internet, I haven't found help for my problem...

Ok to my problem: I have two JSpinner, that show the current Hour and Minutes. They look like that:

Date date = new Date();
SpinnerDateModel sm = new SpinnerDateModel(date, null, null, Calendar.HOUR_OF_DAY);
TimeBeginSpinner = new javax.swing.JSpinner(sm);
JSpinner.DateEditor de = new JSpinner.DateEditor(TimeBeginSpinner, "HH:mm");
TimeBeginSpinner.setEditor(de);

Now to my question. I am trying to calculate the difference between these two time periods. But that does not always seem to work.

I tried two different versions, without an external lib and with JodaTime.

Without lib, i calculate it like that:

long diff = dateEnd.getTime() - dateBegin.getTime();

long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = 24 + diff / (60 * 60 * 1000) % 24;
long diffDays = diff / (24 * 60 * 60 * 1000) + 16753;

dateEnd and dateBegin are the Dates i received from the JSpinner.

With JodaTime, i tried it like this:

Duration duration = new Duration(dateBegin.getTime(), dateEnd.getTime());

System.out.println("JodaTime Days: " + duration.getStandardDays());
System.out.println("JodaTime Hours: " + duration.getStandardHours());
System.out.println("JodaTime Minutes: " + duration.getStandardMinutes());

Now here comes the problem: If I calculate the difference without touching the JSpinner, it seems to work. Here my log:

JodaTime Days: 0
JodaTime Hours: 0
JodaTime Minutes: 0
calcTimeDiff: ---------------------------
getTime Begin: 1447617955831
getTime End: 1447617955831
diffDays: 0
diffHours: 0
diffMinutes: 0

Once I touch the second JSpinner and want to check if it calculates 1 hour difference correctly, something goes wrong... Here my log: JSpinner1 is still untouched, JSpinner2 is set one hour later.

JodaTime Days: -16753
JodaTime Hours: -402095
JodaTime Minutes: -24125700
calcTimeDiff: ---------------------------
getTime Begin: 1447617955831
getTime End: 75900000
diffDays: -16753
diffHours: 1
diffMinutes: 0

Turning JSpinner1 down 1 hour, should make a difference of 2 hours.

Log:

JodaTime Days: 0
JodaTime Hours: 2
JodaTime Minutes: 120
calcTimeDiff: ---------------------------
getTime Begin: 69060000
getTime End: 76260000
diffDays: 0
diffHours: 26
diffMinutes: 0

Does anyone know what goes wrong here?

Thanks for every help!

Felicce
  • 25
  • 1
  • 1
  • 6
  • Your Question is unclear. You do not provide enough code nor example data values to determine the problem. For example, you have a large number "Begin" `1447617955831` which is presumably a count-from-epoch in milliseconds while you have suspiciously smaller number "End" `75900000`, so that does not make sense. – Basil Bourque Nov 16 '15 at 02:58
  • Oh ok I'm sorry, the large Number "Begin" and "End" are variables from the JSpinners. The first one is "Begin" and the second "End". Both of them are Dates. so if I call getTime(), I receive a long. (Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.) And if i change only one JSpinner it seems to change the whole Date, not only 1 hour.. Does that make it a little clearer :)? – Felicce Nov 16 '15 at 12:45
  • Reread my comment, and look very carefully at the numbers I cite. – Basil Bourque Nov 16 '15 at 14:23
  • yes I know what you mean and I am confused about that too... Could it be, that changing the Spinner, changes the Date completely? It is initiated with Date date = new Date(). so Millisecs from 1970. Is it possible, that changing the Spinner, changes the start Date, so it doesn't start at 1970 anymore? Do you know what I mean? Sorry about my English.. – Felicce Nov 16 '15 at 15:10

0 Answers0