So che questa discussione è di due anni ormai, io ancora non vedo una risposta corretta qui.
A meno che non si desideri utilizzare Joda o Java 8 e se è necessario eseguire le date in base all'ora legale.
Quindi ho scritto la mia soluzione. L'aspetto importante è che funziona solo se si ha realmente a cuore solo di date perché è necessario eliminare le informazioni sul tempo, quindi se volete qualcosa di simile 25.06.2014 - 01.01.2010 = 1636
, questo dovrebbe funzionare indipendentemente dalla DST:
private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd.MM.yyyy");
public static long getDayCount(String start, String end) {
long diff = -1;
try {
Date dateStart = simpleDateFormat.parse(start);
Date dateEnd = simpleDateFormat.parse(end);
//time is always 00:00:00 so rounding should help to ignore the missing hour when going from winter to summer time as well as the extra hour in the other direction
diff = Math.round((dateEnd.getTime() - dateStart.getTime())/(double) 86400000);
} catch (Exception e) {
//handle the exception according to your own situation
}
return diff;
}
Poiché il tempo è sempre 00:00:00
, usando il doppio e poi Math.round()
dovrebbe aiutare a ignorare i 60000ms mancanti (1 ora) quando si passa dall'inverno all'ora legale e l'ora in più se si passa dall'estate all'inverno.
Questo è un piccolo test JUnit4 Io lo uso per dimostrarlo:
@Test
public void testGetDayCount() {
String startDateStr = "01.01.2010";
GregorianCalendar gc = new GregorianCalendar(locale);
try {
gc.setTime(simpleDateFormat.parse(startDateStr));
} catch (Exception e) {
}
for (long i = 0; i < 10000; i++) {
String dateStr = simpleDateFormat.format(gc.getTime());
long dayCount = getDayCount(startDateStr, dateStr);
assertEquals("dayCount must be equal to the loop index i: ", i, dayCount);
gc.add(GregorianCalendar.DAY_OF_YEAR, 1);
}
}
... o se volete vedere quello che fa 'la vita', sostituire l'affermazione con un semplice:
System.out.println("i: " + i + " | " + dayCount + " - getDayCount(" + startDateStr + ", " + dateStr + ")");
... e questo è ciò che l'uscita dovrebbe essere simile:
i: 0 | 0 - getDayCount(01.01.2010, 01.01.2010)
i: 1 | 1 - getDayCount(01.01.2010, 02.01.2010)
i: 2 | 2 - getDayCount(01.01.2010, 03.01.2010)
i: 3 | 3 - getDayCount(01.01.2010, 04.01.2010)
...
i: 1636 | 1636 - getDayCount(01.01.2010, 25.06.2014)
...
i: 9997 | 9997 - getDayCount(01.01.2010, 16.05.2037)
i: 9998 | 9998 - getDayCount(01.01.2010, 17.05.2037)
i: 9999 | 9999 - getDayCount(01.01.2010, 18.05.2037)
sembra che questo metodo è esclusiva e non incluso, quindi potrebbe voler aggiungere 1 al risultato (o aggiungere ** 25 ** (e non 24) ore a 'date2') per renderlo inclusivo. –
@AlaaM .: Bene, * se * vuoi renderlo inclusivo. Se mi chiedessero il numero di giorni tra il 24 dicembre e il 25 dicembre, direi 1, non 2 ... (e l'OP sembra abbastanza felice). –
Sì, è per questo che ho detto * potrebbe volere *. Nel mio caso dovevo visualizzare "giorni rimanenti fino alla scadenza". Quindi, a mio parere questo caso dovrebbe essere inclusiva –