Failed to set calendar working time

Hi,


I’d like to set working time from 8:00 to 11:00 and from 13:00 to 16:00, but it doesn’t work, please see the attachment.

Below is the code:

public static void main(String[] args) throws ParseException {
FileInputStream fstream = null;

try {
// Create a stream object containing the license file
fstream = new FileInputStream(“Aspose.Tasks.lic”);
// Instantiate the License class
License license = new License();
// Set the license through the stream object
license.setLicense(fstream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fstream.close();
} catch (IOException e) {
e.printStackTrace();
}
}

Project asProject = new Project(“Empty_Project.mpp”);

// Define Calendar
Calendar cal = asProject.getCalendars().add(“Calendar1”);
Calendar.makeStandardCalendar(cal);
for (WeekDay wd : cal.getWeekDays()) {
if(wd.getDayWorking()) {
WorkingTime wt1 = new WorkingTime();
java.util.Calendar calTime = java.util.Calendar.getInstance();
calTime.set(1,1,1,8,0,0);
wt1.setFromTime(calTime.getTime());
calTime.set(1,1,1,11,0,0);
wt1.setToTime(calTime.getTime());
WorkingTime wt2 = new WorkingTime();
calTime.set(1,1,1,13,0,0);
wt2.setFromTime(calTime.getTime());
calTime.set(1,1,1,16,0,0);
wt2.setToTime(calTime.getTime());
wd.getWorkingTimes().add(wt1);
wd.getWorkingTimes().add(wt2);
wd.setDayWorking(true);
}
}

asProject.set(Prj.CALENDAR, cal);
asProject.save(“output_test_set_calendar2.mpp”, SaveFileFormat.MPP);
}

zhencheng.guo
Best Regards!

Hi Zhencheng,

Thank you for writing to Aspose support team again.

When we make a calendar standard, its weekdays are automatically populated with the working times. We just have to update the existing time rather than adding new working time. Please give a try to the following modified sample code and let us know the feedback.

Project asProject = new Project("Empty_Project.mpp");
// Define Calendar
Calendar cal = asProject.getCalendars().add("Calendar1");
Calendar.makeStandardCalendar(cal);
for (WeekDay wd : cal.getWeekDays()) {
    if(wd.getDayWorking()) {
        // WorkingTime wt1 = new WorkingTime();
        WorkingTime wt1 = wd.getWorkingTimes().toList().get(0);
        java.util.Calendar calTime = java.util.Calendar.getInstance();
        calTime.set(1,1,1,8,0,0);
            wt1.setFromTime(calTime.getTime());
            calTime.set(1,1,1,11,0,0);
            wt1.setToTime(calTime.getTime());
            WorkingTime wt2 = wd.getWorkingTimes().toList().get(1);
        calTime.set(1,1,1,13,0,0);
            wt2.setFromTime(calTime.getTime());
            calTime.set(1,1,1,16,0,0);
            wt2.setToTime(calTime.getTime());
            // wd.getWorkingTimes().add(wt1);
            // wd.getWorkingTimes().add(wt2);
            // wd.setDayWorking(true);
    }
}
asProject.set(Prj.CALENDAR, cal);
asProject.save("output_test_set_calendar2.mpp", SaveFileFormat.MPP);