Calculate Task Duration in Different Units
It can be useful to calculate the duration of a task in different units such as minutes, hours, etc.
Calculating Durations The Task class offers provides the getDuration() method for calculating task duration in different units. This method takes TimeUnitType as the input argument and returns the duration as a double value.
The following piece of code shows how to use this method to retrieve a task’s duration in different units: minute, day, hour, week and month.
1// For complete examples and data files, please go to https://github.com/aspose-tasks/Aspose.Tasks-for-Java 2// The path to the documents directory. 3String dataDir = Utils.getDataDir(TaskDurationInDifferentUnits.class); 4 5// Read MS Project template file 6String fileName = dataDir + "SampleProject.mpp"; 7 8// Read the input file as Project 9Project project = new Project(fileName); 10 11// Get a task to calculate its duration in different formats 12Task task = project.getRootTask().getChildren().getById(1); 13 14// Get the duration in Minutes 15double mins = task.get(Tsk.DURATION).convert(TimeUnitType.Minute).toDouble(); 16// Get the duration in Days 17double days = task.get(Tsk.DURATION).convert(TimeUnitType.Day).toDouble(); 18// Get the duration in Hours 19double hours = task.get(Tsk.DURATION).convert(TimeUnitType.Hour).toDouble(); 20// Get the duration in Weeks 21double weeks = task.get(Tsk.DURATION).convert(TimeUnitType.Week).toDouble(); 22// Get the duration in Months 23double months = task.get(Tsk.DURATION).convert(TimeUnitType.Month).toDouble();