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