0

I usually deal with dates with ints according to the pattern yyyyMMdd.
e.g.: today is 20170113, or better 20,170,113.
I need to calculate the distance bethween dates in days, so I wrote this:

    public static int calculateDistance(int data1, int data2){
        try {
            SimpleDateFormat normalDateFormat = new SimpleDateFormat("yyyyMMdd");
            long beginTime = normalDateFormat.parse(String.valueOf(data1)).getTime();
            long endTime = normalDateFormat.parse(String.valueOf(data2)).getTime();
            long diff = endTime - beginTime;
            return (int) TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
    }

And this test:

    public void testCalculateDistance() {
        System.out.println(Calculator.calculateDistance(20090325, 20090328));
        System.out.println(Calculator.calculateDistance(20090326, 20090329));
        System.out.println(Calculator.calculateDistance(20090327, 20090330));
        System.out.println(Calculator.calculateDistance(20090328, 20090331));
        System.out.println(Calculator.calculateDistance(20090329, 20090401));
        System.out.println(Calculator.calculateDistance(20090330, 20090402));
        System.out.println(Calculator.calculateDistance(20090331, 20090403));
        System.out.println(Calculator.calculateDistance(20090401, 20090404));
}

The output should always be the same, since I'm making the same modifications to both begin and end. But I get 2 (so 1 day less) iff the interval (end excluded) contains March 29.
I also found it happens whenever I have this day of any year within the interval I mean to measure.
Why does it happen?
How can I fix it?

EDIT: I read this and I know how to calculate the difference between dates. The point is that this doesn't seem to work with this kind of classes, so this question is not a duplicate because I needn't know how to find the difference, but I need to know which classes I should use, instead of the ones I used to use.

Community
  • 1
  • 1
SamCle88
  • 275
  • 3
  • 9
  • 22
  • 5
    You might want to start dealing with dates as instances of `LocalDate` (and `LocalDateTime`) instead. – Kayaman Jan 13 '17 at 10:36
  • 2
    Read this : http://stackoverflow.com/questions/1969442/whats-wrong-with-java-date-time-api – Sanjit Kumar Mishra Jan 13 '17 at 10:36
  • 3
    https://www.timeanddate.com/time/dst/2009.html ... look what happens on 29th of March. So your issue is the switch from CET to CEST and that one day is only 23h long and doesn't count as a whole day. – Tom Jan 13 '17 at 10:47

1 Answers1

3

In this case it is probably preferable to use LocalDate and ChronoUnit from java.time package.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDate beginning = LocalDate.parse("20090325",formatter);
LocalDate ending = LocalDate.parse("20090328",formatter);
System.out.println(ChronoUnit.DAYS.between(beginning,ending));
Anton Balaniuc
  • 10,889
  • 1
  • 35
  • 53
  • 1
    Agree. The code in the question seems to have a problem with changing to summer time (DST). `LocalDate` avoids that problem (some clever use of `GregorianCalendar` might too, but that would get more complicated, so why bother?) – Ole V.V. Jan 13 '17 at 11:20