Reading Calendar Information

Retrieving Calendar Information

Microsoft Project lets users associate a variety of different calendars with working time, tasks and resources. These features are also available in Aspose.Tasks. This article explains how to retrieve information about which calendars are used in a project. Aspose.Tasks can retrieve calendar information for all versions of Microsoft Project: 2003, 2007, 2010, and 2013.

The CalendarS property exposed by the Project class supports List of Aspose.Tasks.Calendar objects. This property can be used to retrieve the calendar information carried by the project.

To view calendar information in Microsoft Project:

  1. Open a project.
  2. From the Project menu, select Change Working Time.

The following piece of code retrieves the calendar information from a project.

 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(RetrieveCalendarInfo.class);
 4
 5long OneSec = 10000000;// microsecond * 10
 6long OneMin = 60 * OneSec;
 7long OneHour = 60 * OneMin;
 8
 9// Create a project reader instance
10Project project = new Project(dataDir + "input.mpp");
11
12// Retrieve Calendars Information
13CalendarCollection alCals = project.getCalendars();
14
15for (Calendar cal : alCals) {
16    if (cal.getName() != null) {
17        System.out.println("Calendar UID : " + cal.getUid());
18        System.out.println("Calendar Name : " + cal.getName());
19
20        WeekDayCollection alDays = cal.getWeekDays();
21        for (WeekDay wd : alDays) {
22            double ts = wd.getWorkingTime(); // milliseconds
23            double time = ts / (OneHour);
24            if (wd.getDayWorking()) {
25                System.out.print(wd.getDayType() + ":");
26                System.out.print("Working Time:" + time + " Hours");
27                System.out.println(", Ticks = " + ts);
28            }
29        }
30    }
31}
32
33// Display result of conversion.
34System.out.println("Process completed Successfully");

Reading Work Weeks Information from Calendar

Aspose.Tasks for Java API can read Work weeks information from any of the Project’s calendar. The WorkWeek class can be used to achieve this.

 1// The path to the documents directory.
 2String dataDir = Utils.getDataDir(ReadWorkWeeks.class);
 3
 4// Create project instance and access calendar and work weeks collection
 5Project project = new Project(dataDir + "ReadWorkWeeksInformation.mpp");
 6Calendar calendar = project.getCalendars().getByUid(3);
 7WorkWeekCollection collection = calendar.getWorkWeeks();
 8
 9for (WorkWeek workWeek : collection) {
10    // Display work week name, from and to dates
11    System.out.println(workWeek.getName());
12    System.out.println(workWeek.getFromDate());
13    System.out.println(workWeek.getToDate());
14
15    // This data is all about "Details." button you can set special working times
16    // for special WeekDay or even make it non-working
17    WeekDayCollection weekDays = workWeek.getWeekDays();
18    for (WeekDay day : weekDays) {
19        // You can further traverse through working times and display these
20        WorkingTimeCollection workingTimes = day.getWorkingTimes();
21    }
22}

Getting Working Hours On or Between Dates

Working hours are typically defined as a range of hours over a day, on specific days of the week. For example, 09:00 – 17:00, Monday to Friday. With Aspose.Tasks for Java, it is possible to calculate the number of working hours that fall on a date, or between two dates. Finding out the number of working dates between two dates help project managers figure out whether there are enough hours to get work done, or if they need to find more resources to complete a task.

Calculating Working Hours

The Calendar class exposes two overloaded methods for finding working hours on or between dates:

The following example shows how to find the duration in minutes, hours and days between two dates.

 1// The path to the documents directory.
 2String dataDir = Utils.getDataDir(GetWorkingHours.class);
 3
 4long OneSec = 10000000;//microsecond * 10
 5long OneMin = 60 * OneSec;
 6long OneHour = 60 * OneMin;
 7
 8Project project = new Project(dataDir + "BaselineTask.mpp");
 9Task task = project.getRootTask().getChildren().getById(1);
10
11Calendar taskCalendar = task.get(Tsk.CALENDAR);
12
13java.util.Calendar calStartDate = java.util.Calendar.getInstance();
14calStartDate.setTime(task.get(Tsk.START));
15
16java.util.Calendar calEndDate = java.util.Calendar.getInstance();
17calEndDate.setTime(task.get(Tsk.FINISH));
18
19java.util.Calendar tempDate = java.util.Calendar.getInstance();
20tempDate = calStartDate;
21
22Resource resource = project.getResources().getById(1);
23Calendar resourceCalendar = resource.get(Rsc.CALENDAR);
24
25//TimeSpan timeSpan;
26long timeSpan = 0;
27
28//Get Duration in Minutes
29double durationInMins = 0;
30
31while (tempDate.before(calEndDate))
32{
33    if (taskCalendar.isDayWorking(tempDate.getTime()) && resourceCalendar.isDayWorking(tempDate.getTime()))
34    {
35        timeSpan = (long) taskCalendar.getWorkingHours(tempDate.getTime());
36        durationInMins = durationInMins + (timeSpan / OneMin);
37    }
38    tempDate.add(java.util.Calendar.DATE, 1);
39}
40tempDate.setTime(task.get(Tsk.START));
41
42//Get Duration in Hours
43double durationInHours = 0;
44
45while (tempDate.before(calEndDate))
46{
47    if (taskCalendar.isDayWorking(tempDate.getTime()) && resourceCalendar.isDayWorking(tempDate.getTime()))
48    {
49        timeSpan = (long) taskCalendar.getWorkingHours(tempDate.getTime());
50        durationInHours = durationInHours + (timeSpan / OneHour);
51    }
52    tempDate.add(java.util.Calendar.DATE, 1);
53}
54tempDate.setTime(task.get(Tsk.START));
55
56//Get Duration in Days
57double durationInDays = 0;
58
59while (tempDate.before(calEndDate))
60{
61    if (taskCalendar.isDayWorking(tempDate.getTime()) && resourceCalendar.isDayWorking(tempDate.getTime()))
62    {
63        timeSpan = (long) taskCalendar.getWorkingHours(tempDate.getTime());
64        if ((timeSpan / OneHour) > 0)
65            durationInDays = durationInDays + (timeSpan/OneHour/8.0);
66    }
67    tempDate.add(java.util.Calendar.DATE, 1);
68}
69tempDate = calStartDate;
70
71System.out.println("Duration in Minutes = " + durationInMins);
72System.out.println("Duration in Hours = " + durationInHours);
73System.out.println("Duration in Days = " + durationInDays);
74System.out.println();
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.