1 /**
2 * Copyright 2012 The Kuali Foundation Licensed under the
3 * Educational Community License, Version 2.0 (the "License"); you may
4 * not use this file except in compliance with the License. You may
5 * obtain a copy of the License at
6 *
7 * http://www.osedu.org/licenses/ECL-2.0
8 *
9 * Unless required by applicable law or agreed to in writing,
10 * software distributed under the License is distributed on an "AS IS"
11 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12 * or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 *
15 */
16 package org.kuali.student.enrollment.class2.acal.util;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.apache.commons.lang.time.DateFormatUtils;
20 import org.joda.time.MutableDateTime;
21 import org.kuali.student.enrollment.class2.acal.dto.TimeSetWrapper;
22 import org.kuali.student.r2.common.dto.RichTextInfo;
23
24 import java.text.SimpleDateFormat;
25 import java.util.Date;
26
27 /**
28 * This class //TODO ...
29 *
30 * @author Kuali Student Team
31 */
32 public class CommonUtils {
33 public static void assembleTimeSet(TimeSetWrapper timeSetWrapper, Date startDate, Date endDate) throws Exception{
34 SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm aa");
35 if (startDate !=null) {
36 String startDateFullString = formatter.format(startDate);
37 String[] timeStr = startDateFullString.split(" ");
38 timeSetWrapper.setStartDate(new SimpleDateFormat("MM/dd/yyyy").parse(timeStr[0]));
39 if (!"12:00".equals(timeStr[1])){
40 timeSetWrapper.setStartTime(timeStr[1]);
41 }
42 timeSetWrapper.setStartTimeAmPm(timeStr[2].toLowerCase());
43 }
44
45 if (endDate !=null) {
46 String endDateFullString = formatter.format(endDate);
47 String[] timeStr = endDateFullString.split(" ");
48 timeSetWrapper.setEndDate(new SimpleDateFormat("MM/dd/yyyy").parse(timeStr[0]));
49 if (!"12:00".equals(timeStr[1])){
50 timeSetWrapper.setEndTime(timeStr[1]);
51 }
52 timeSetWrapper.setEndTimeAmPm(timeStr[2].toLowerCase());
53
54 }
55 }
56
57 public static boolean isValidDateRange(Date startDate,Date endDate){
58 if(startDate != null && endDate != null) {
59 if (startDate.after(endDate) || endDate.before(startDate)){
60 return false;
61 }
62 }
63 return true;
64 }
65
66 public static String formatDate(Date date){
67 return DateFormatUtils.format(date, CalendarConstants.DEFAULT_DATE_FORMAT);
68 }
69
70 public static boolean isDateWithinRange(Date startDate,Date endDate,Date checkDate){
71 if(startDate != null && endDate != null && checkDate != null) {
72 if (checkDate.before(startDate) || checkDate.after(endDate)){
73 return false;
74 }
75 }
76 return true;
77 }
78
79 public static RichTextInfo buildDesc(String descr){
80 RichTextInfo rti = new RichTextInfo();
81 rti.setPlain(descr);
82 return rti;
83 }
84
85 /* not currently used
86 public static Date getStartDate(TimeSetWrapper timeSetWrapper) throws Exception {
87 Date startDate = timeSetWrapper.getStartDate();
88 String startTime = timeSetWrapper.getStartTime();
89 String amPm = timeSetWrapper.getStartTimeAmPm();
90 Date fullStartDate = getDateWithTime(startDate, startTime, amPm);
91
92 return fullStartDate;
93 }
94 public static Date getEndDate(TimeSetWrapper timeSetWrapper) throws Exception {
95 Date endate = timeSetWrapper.getEndDate();
96 String endTime = timeSetWrapper.getEndTime();
97 String amPm = timeSetWrapper.getEndTimeAmPm();
98 Date fullEndDate = getDateWithTime(endate, endTime, amPm);
99
100 return fullEndDate;
101 } */
102
103 /* formatter.parse throws an Exception which forces try/catch on everything - ugh
104 public static Date getDateWithTime(Date adate, String atime, String ampm) throws Exception {
105 Date fullDate = null;
106 if (ampm == null)
107 ampm = "am";
108
109 if(adate != null){
110 if(atime != null && !atime.isEmpty()){
111 String fullDateString = new SimpleDateFormat("MM/dd/yyyy").format(adate);
112 fullDateString = fullDateString.concat(" " + atime + " " + ampm);
113 SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm aa");
114 fullDate = formatter.parse(fullDateString);
115 }
116 else {
117 fullDate = adate;
118 }
119 }
120 return fullDate;
121 }*/
122
123 public static Date getDateWithTime(Date date, String hourMinute, String amPm) {
124 if (null == date) {
125 return null;
126 }
127
128 MutableDateTime dateTime = new MutableDateTime(date);
129
130 if (StringUtils.isNotBlank(hourMinute)) {
131 int hour = Integer.parseInt(StringUtils.substringBefore(hourMinute,":"));
132 if (StringUtils.equalsIgnoreCase(amPm,"PM")) {
133 // 24-hour clock; e.g. 12 PM = hour 12; 8 PM = hour 20
134 if (hour < 12) {
135 hour += 12;
136 }
137 }
138 else // if amPm is blank/null, assume AM
139 if (hour == 12) {
140 hour = 0; // 12 AM is stored in Calendar as hour 0
141 }
142 dateTime.setTime(hour, Integer.parseInt(StringUtils.substringAfter(hourMinute,":")), 0, 0);
143 }
144
145 return dateTime.toDate();
146 }
147
148 }