0

I am trying to compute the duration Of each Project and each Task appointed to the Project, unfortunately it always returns 0. computeDuration:

        Date minDate = projectTasks[0].gettaskFromDate();
        Date maxDate = projectTasks[0].gettaskEndDate();
        for (int i = 0; i < numOfTasks; ++i) {
            if (projectTasks[i] != null && projectTasks[i].gettaskEndDate() != null && projectTasks[i].gettaskFromDate() != null) {
                projectTasks[i].settaskDuration(utils.Globals.computeDuration(projectTasks[i].gettaskFromDate(), projectTasks[i].gettaskEndDate()));
            }
        }
        for (int i = 0; i < numOfTasks; ++i) {
            if (projectTasks[i] != null && projectTasks[i].gettaskEndDate() != null && projectTasks[i].gettaskFromDate() != null) {
                if (utils.Globals.validateDates(projectTasks[i].gettaskFromDate(), minDate)) {
                    minDate = projectTasks[i].gettaskFromDate();
                }
                if (utils.Globals.validateDates(maxDate, projectTasks[i].gettaskEndDate())) {
                    maxDate = projectTasks[i].gettaskEndDate();
                }
            }
        }
        setprojectDuration(utils.Globals.computeDuration(minDate, maxDate));
    }

utils.Globals.validateDates:

public static boolean validateDates(Date from, Date to){
        return from.before(to);
    }

utils.Globals.computeDuration:

 public static int computeDuration(Date from, Date to){
        long diffIn = Math.abs(to.getTime() - from.getTime());
        int duration = (int) TimeUnit.DAYS.convert(diffIn, TimeUnit.MILLISECONDS);
        return duration;
    }

Thanks in advance!

6
  • 3
    Why are you using the obsolete Date class and not LocalDate? Edit your question to include a minimal reproducible example that we can copy into our IDE, compile, and run hundreds of tests to determine the cause of the problem. Commented Mar 31, 2023 at 11:22
  • Its for a project for my uni and I am obliged to use the Date format.I am well aware that LocalDate is far more useful. Also, my project uses 7 classes with each one intertwined so im having a rough time creating a minimal reproducible example. Commented Mar 31, 2023 at 11:53
  • 1
    @dannyph Could you please nofify your tutor that they're using an obsolete class that was supplanted a decade ago? Commented Mar 31, 2023 at 12:56
  • 1
    Please provide an example of Date arguments which you believe should produce a duration greater than zero days, but which are instead causing zero to be returned. Commented Mar 31, 2023 at 14:10
  • 1
    And yes, teachers requiring their students to use Date in 2023 deserve to be sacked. It’s really nothing useful to teach. Commented Apr 4, 2023 at 9:58

1 Answer 1

0

You could confirm if the diff between your from and to dates is over one day or not. If all diffs is less than one day, then computeDuration return 0 always.

Please check DAYS diff is expected or not in the blow codes.

 public static int computeDuration(Date from, Date to){
        long diffIn = Math.abs(to.getTime() - from.getTime());
        int duration = (int) TimeUnit.DAYS.convert(diffIn, TimeUnit.MILLISECONDS);
        return duration;
    }
Sign up to request clarification or add additional context in comments.

4 Comments

I checked the given front end and i the difference between dates is always more than a day
Print out the from date, to dates and duration in computeDuration method. Then confirm if they are expected or not. I guess there is no magic here.
Here is a part of the print: 0 EVENFLOW 490000.0 0 PENDING TaskID Title Start Date End Date Duration Status 0.1 Dynamic Maintenance of Neural Models 23/03/2023 01/11/2023 0 PENDING 0.2 Formal Verification Models 01/04/2023 29/09/2023 0 COMPLETED 1 CREXDATA 1800000.58 0 PENDING
I mean the parameters of method computeDuration. Anyway, try 2 numOfTasks, debug or print the intermediate variables to verify the values. BTW, Did you see any exception threw when you run your code?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.