1
2
3
4
5
6
7
8
9
10
11
12
13
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
29
30
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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
134 if (hour < 12) {
135 hour += 12;
136 }
137 }
138 else
139 if (hour == 12) {
140 hour = 0;
141 }
142 dateTime.setTime(hour, Integer.parseInt(StringUtils.substringAfter(hourMinute,":")), 0, 0);
143 }
144
145 return dateTime.toDate();
146 }
147
148 }