Creating, Updating and Removing Calendar
Creating Calendars
Calendars and other information is used by Microsoft Project to build project schedules. With Aspose.Tasks for Java, adding a calendar to a project is easy. The Calendar class constructors allow you to assign a calendar name to the calendar. The options are described below.
There are three ways of creating a calendar in Aspose.Tasks. The Calendar class exposes the following constructor:
- Calendar(string name) – Creates a calendar with a name.
Project calendars are returned as CalendarCollection by the getCalendars() method of Project class. When you open the output file in Microsoft Project and, on the Project menu, select Project Information to access the calendars.
Project information showing the calendars info
Programming Sample
1// The path to the documents directory.
2String dataDir = Utils.getDataDir(CreateCalendar.class);
3
4// Create a project instance
5Project prj = new Project();
6
7// Define Calendar
8Calendar cal1 = prj.getCalendars().add("no info");
9Calendar cal2 = prj.getCalendars().add("no name");
10Calendar cal3 = prj.getCalendars().add("cal3");
11
12prj.getCalendars().add(cal1);
13prj.getCalendars().add(cal2);
14prj.getCalendars().add(cal3);
15
16prj.save(dataDir + "Project.Xml", SaveFileFormat.XML);
17
18// Display result of conversion.
19System.out.println("Process completed Successfully");
Defining Weekdays for Calendar
Microsoft Project keeps track of which days are considered weekdays in a calendar so that it can calculate project end dates and so on. Aspose.Tasks allows developers to define weekdays for a calendar associated with a project. This topic shows how.
The Days collection exposed by the Calendar class is used to define the weekdays for a calendar. The Days collection represents an array list of WeekDay objects.
The CreateDefaultWorkingDay method exposed by the WeekDay class can further be implemented to define the default day timings, while the FromTime and ToTime properties exposed by the WorkingTime class is used to define the specific timing for a day. The FromTime and ToTime support the DateTime datatype.
The code samples below set weekdays for a project. After running the code, open the output file in Microsoft Project and, on the Tools menu, select Change Work Timing to establish that the changes have been applied.
Defining weekdays in Microsoft Project
Programming Sample
The code below defines Monday through Thursday as weekdays with default timings, whereas Friday as a weekday with special timings.
1// The path to the documents directory.
2String dataDir = Utils.getDataDir(DefineWeekdays.class);
3
4//Create a project instance
5Project prj = new Project();
6
7//Define Calendar
8Calendar cal = prj.getCalendars().add("Calendar1");
9
10//Add working days Monday through Thursday with default timings
11cal.getWeekDays().add(WeekDay.createDefaultWorkingDay(DayType.Monday));
12cal.getWeekDays().add(WeekDay.createDefaultWorkingDay(DayType.Tuesday));
13cal.getWeekDays().add(WeekDay.createDefaultWorkingDay(DayType.Wednesday));
14cal.getWeekDays().add(WeekDay.createDefaultWorkingDay(DayType.Thursday));
15cal.getWeekDays().add(new WeekDay(DayType.Saturday));
16cal.getWeekDays().add(new WeekDay(DayType.Sunday));
17
18//Set Friday as short working day
19WeekDay myWeekDay = new WeekDay(DayType.Friday);
20
21//Sets working time. Only time part of DateTime is important
22WorkingTime wt1 = new WorkingTime();
23java.util.Calendar calTime = java.util.Calendar.getInstance();
24
25calTime.set(1,1,1,9,0,0);
26Date date = calTime.getTime();
27wt1.setFromTime(date);
28
29calTime.set(1,1,1,12,0,0);
30date = calTime.getTime();
31wt1.setToTime(date);
32
33WorkingTime wt2 = new WorkingTime();
34
35calTime.set(1,1,1,13,0,0);
36date = calTime.getTime();
37wt2.setFromTime(date);
38
39calTime.set(1,1,1,16,0,0);
40date = calTime.getTime();
41wt2.setToTime(date);
42
43myWeekDay.getWorkingTimes().add(wt1);
44myWeekDay.getWorkingTimes().add(wt2);
45myWeekDay.setDayWorking(true);
46cal.getWeekDays().add(myWeekDay);
47
48
49//Save the Project
50prj.save(dataDir + "Project.Xml", SaveFileFormat.XML);
51
52//Display result of conversion.
53System.out.println("Process completed Successfully");
Replacing a Calendar
Calendars are used to see whether resources are available, and when tasks are scheduled. There are different types of calendars. This topic looks at how to replace a base calendar, that is, the calendar used for projects and tasks, with another one.
Base calendars come in different forms:
- Standard: the default calendar has a Monday to Friday work week and a day that runs from 08:00 to 17:00.
- 24-hours: used for around the clock working, or for resources that run around the clock.
- Night shift: similar to the standard calendar, the night-shift calendar has a Monday to Saturday morning work week and a day that runs from 23:00 to 08:00.
Replacing a Calendar with a New Calendar
The Project class contains CalendarCollection that comprises of all the calendars which are part of the project. A Calendar item can be removed and a new can be added as well, thus replacing the older one.
Programming Sample
The following lines of code replace an existing calendar with a new standard calendar.
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(ReplaceCalendar.class);
4
5Project project = new Project();
6// Add a calendar to the project
7Calendar cal = project.getCalendars().add("Cal 1");
8
9Calendar newCal = new Calendar("New Cal");
10CalendarCollection calColl = project.getCalendars();
11for (Calendar c : calColl) {
12 if (c.getName() == "Cal 1") {
13 calColl.remove(c);
14 calColl.add("Standard", newCal);
15 break;
16 }
17}
18// Display result of conversion.
19System.out.println("Process completed Successfully");
Making a Standard Calendar
A standard calendar provides the most common work days, work hours and holidays. By default, a standard calendar is added whenever a project is created using Microsoft Project. Aspose.Tasks has features for defining a standard calendar for a project.
The Calendar class exposes the following two static (shared in Visual Basic) overloaded methods for creating a standard calendar:
- makeStandardCalendar() – Creates a new standard calendar.
- makeStandardCalendar(Calendar cal) – Makes a calendar as standard.
In Microsoft Project, open the file output by the code below and, on the Tools menu, select Change Working Times Information, to see the standard calendar.
The Change Working Times Information dialog shows the standard calendar used by the project
Programming Sample
The following lines of code create a standard calendar using both methods.
1// The path to the documents directory.
2String dataDir = Utils.getDataDir(MakeStandardCalendar.class);
3
4//Create a project instance
5Project project = new Project();
6
7//Define Calendar and make it standard
8Calendar cal1 = project.getCalendars().add("My Cal");
9Calendar.makeStandardCalendar(cal1);
10
11//Save the Project
12project.save(dataDir + "Project.Xml", SaveFileFormat.XML);
13
14//Display result of conversion.
15System.out.println("Process completed Successfully");
Writing Updated Calendar Data to MPP
With Aspose.Tasks for Java, you can update calendar data in a Microsoft Project MPP file and save it back. This topic shows an example of adding a calendar to the MPP file, update it and save back the changes to the MPP file.
The following code shows how to update the calendar data of a project by adding a new calendar and saving it back to the original MPP file. The steps involved in this activity are:
- Create an instance of the Project class.
- Read the source MPP file using the constructor.
- Add the calendar data to the project.
- Save the updated project data back to the MPP file.
Programming Sample
1// The path to the documents directory.
2String dataDir = Utils.getDataDir(UpdatedCalendarToMpp.class);
3
4String resultFile = "OutputMPP.mpp";
5String newFile = "SampleMPP.mpp";
6
7Project project = new Project(dataDir + newFile);
8Calendar cal1 = project.getCalendars().add("Calendar 1");
9GetTestCalendar(cal1);
10
11project.set(Prj.CALENDAR, cal1);
12
13//Save the Project
14project.save(dataDir + resultFile, SaveFileFormat.MPP);
15
16//Display result of conversion.
17System.out.println("Process completed Successfully");