View Javadoc
1   /*
2    * Kuali Coeus, a comprehensive research administration system for higher education.
3    * 
4    * Copyright 2005-2015 Kuali, Inc.
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
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       * This method computes the number of months between any 2 given {@link java.sql.Date} objects
36       *
37       * @param dateStart starting date.
38       * @param dateEnd end date.
39       *
40       * @return number of months between the start date and end date.
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      * This method returns a {@link Calendar} whose date matches the date passed as {@link String}
105      *
106      * @param dateStr string in "MM/dd/yyyy" format for which the Calendar value has to be returned.
107      * @return Calendar calendar value corresponding to the date string.
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      * This method is used to get Calendar date for the corresponding date object.
122      *
123      * @param date(Date) date for which Calendar value has to be found.
124      * @return calendar value corresponding to the date.
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 }