1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.kuali.coeus.s2sgen.impl.datetime;
20
21 import org.apache.commons.lang3.StringUtils;
22 import org.kuali.coeus.sys.api.model.ScaleTwoDecimal;
23 import org.springframework.stereotype.Component;
24
25 import java.math.BigDecimal;
26 import java.math.RoundingMode;
27 import java.util.Calendar;
28 import java.util.TimeZone;
29
30 @Component("s2SDateTimeService")
31 public class S2SDateTimeServiceImpl implements S2SDateTimeService {
32
33
34
35
36
37
38
39
40
41
42 @Override
43 public ScaleTwoDecimal getNumberOfMonths(java.util.Date dateStart, java.util.Date dateEnd) {
44 ScaleTwoDecimal monthCount = ScaleTwoDecimal.ZERO;
45 int fullMonthCount = 0;
46
47 Calendar startDate = Calendar.getInstance();
48 Calendar endDate = Calendar.getInstance();
49 startDate.setTime(dateStart);
50 endDate.setTime(dateEnd);
51
52 startDate.clear(Calendar.HOUR);
53 startDate.clear(Calendar.MINUTE);
54 startDate.clear(Calendar.SECOND);
55 startDate.clear(Calendar.MILLISECOND);
56
57 endDate.clear(Calendar.HOUR);
58 endDate.clear(Calendar.MINUTE);
59 endDate.clear(Calendar.SECOND);
60 endDate.clear(Calendar.MILLISECOND);
61
62 if (startDate.after(endDate)) {
63 return ScaleTwoDecimal.ZERO;
64 }
65 int startMonthDays = startDate.getActualMaximum(Calendar.DATE) - startDate.get(Calendar.DATE);
66 startMonthDays++;
67 int startMonthMaxDays = startDate.getActualMaximum(Calendar.DATE);
68 BigDecimal startMonthFraction = BigDecimal.valueOf(startMonthDays).setScale(2, RoundingMode.HALF_UP).divide(BigDecimal.valueOf(startMonthMaxDays).setScale(2, RoundingMode.HALF_UP), RoundingMode.HALF_UP);
69
70 int endMonthDays = endDate.get(Calendar.DATE);
71 int endMonthMaxDays = endDate.getActualMaximum(Calendar.DATE);
72
73 BigDecimal endMonthFraction = BigDecimal.valueOf(endMonthDays).setScale(2, RoundingMode.HALF_UP).divide(BigDecimal.valueOf(endMonthMaxDays).setScale(2, RoundingMode.HALF_UP), RoundingMode.HALF_UP);
74
75 startDate.set(Calendar.DATE, 1);
76 endDate.set(Calendar.DATE, 1);
77
78 while (startDate.getTimeInMillis() < endDate.getTimeInMillis()) {
79 startDate.set(Calendar.MONTH, startDate.get(Calendar.MONTH) + 1);
80 fullMonthCount++;
81 }
82 fullMonthCount = fullMonthCount - 1;
83 monthCount = monthCount.add(new ScaleTwoDecimal(fullMonthCount)).add(new ScaleTwoDecimal(startMonthFraction)).add(new ScaleTwoDecimal(endMonthFraction));
84 return monthCount;
85 }
86
87 @Override
88 public String removeTimezoneFactor(String applicationXmlText) {
89 Calendar cal = Calendar.getInstance();
90 int dstOffsetMilli = cal.get(Calendar.DST_OFFSET);
91 int zoneOffsetMilli = cal.get(Calendar.ZONE_OFFSET);
92 zoneOffsetMilli = cal.getTimeZone().useDaylightTime()?zoneOffsetMilli+dstOffsetMilli:zoneOffsetMilli;
93 int zoneOffset = zoneOffsetMilli/(1000*60*60);
94 String timezoneId = TimeZone.getTimeZone("GMT" + zoneOffset).getID();
95 String offset="+00:00";
96 if(timezoneId.length()>6){
97 offset = timezoneId.substring(timezoneId.length()-6);
98 }
99 String filteredApplicationStr = StringUtils.remove(applicationXmlText, offset);
100 return filteredApplicationStr;
101 }
102
103
104
105
106
107
108
109 @Override
110 public Calendar convertDateStringToCalendar(String dateStr) {
111 Calendar calendar = null;
112 if (dateStr != null) {
113 calendar = Calendar.getInstance();
114 calendar.set(Integer.parseInt(dateStr.substring(6, 10)), Integer.parseInt(dateStr.substring(0, 2)) - 1,
115 Integer.parseInt(dateStr.substring(3, 5)));
116 }
117 return calendar;
118 }
119
120
121
122
123
124
125
126 @Override
127 public Calendar convertDateToCalendar(java.util.Date date) {
128 Calendar calendar = null;
129 if (date != null) {
130 calendar = Calendar.getInstance();
131 calendar.setTime(date);
132 }
133 return calendar;
134 }
135 }