캘린더 정보 읽기
캘린더 정보 검색
Microsoft Project를 통해 사용자는 다양한 캘린더를 작업 시간, 작업 및 리소스와 연관시킬 수 있습니다. 이러한 기능은 Aspose.Tasks 에서도 사용할 수 있습니다. 이 기사에서는 프로젝트에 사용되는 캘린더에 대한 정보를 검색하는 방법을 설명합니다. Aspose.Tasks 는 모든 버전의 Microsoft Project : 2003, 2007, 2010 및 2013의 캘린더 정보를 검색 할 수 있습니다.
Project 클래스에 노출 된 캘린더 속성은 Aspose.Tasks.calendar 객체의 목록을 지원합니다. 이 속성은 프로젝트에서 수행하는 캘린더 정보를 검색하는 데 사용될 수 있습니다.
Microsoft 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:
- getWorkingHours(Date dt) returns a long data type to hold the working hours on a date passed.
- getWorkingHours(Date startdt, Date enddt) returns a WorkUnit object to define the working hours between two 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();