001 /**
002 * Copyright 2012 The Kuali Foundation Licensed under the
003 * Educational Community License, Version 2.0 (the "License"); you may
004 * not use this file except in compliance with the License. You may
005 * obtain a copy of the License at
006 *
007 * http://www.osedu.org/licenses/ECL-2.0
008 *
009 * Unless required by applicable law or agreed to in writing,
010 * software distributed under the License is distributed on an "AS IS"
011 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
012 * or implied. See the License for the specific language governing
013 * permissions and limitations under the License.
014 *
015 */
016 package org.kuali.student.enrollment.class2.acal.util;
017
018 import org.apache.commons.lang.StringUtils;
019 import org.apache.commons.lang.time.DateFormatUtils;
020 import org.joda.time.MutableDateTime;
021 import org.kuali.student.enrollment.class2.acal.dto.TimeSetWrapper;
022 import org.kuali.student.r2.common.dto.RichTextInfo;
023
024 import java.text.SimpleDateFormat;
025 import java.util.Date;
026
027 /**
028 * This class //TODO ...
029 *
030 * @author Kuali Student Team
031 */
032 public class CommonUtils {
033 public static void assembleTimeSet(TimeSetWrapper timeSetWrapper, Date startDate, Date endDate) throws Exception{
034 SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm aa");
035 if (startDate !=null) {
036 String startDateFullString = formatter.format(startDate);
037 String[] timeStr = startDateFullString.split(" ");
038 timeSetWrapper.setStartDate(new SimpleDateFormat("MM/dd/yyyy").parse(timeStr[0]));
039 if (!"12:00".equals(timeStr[1])){
040 timeSetWrapper.setStartTime(timeStr[1]);
041 }
042 timeSetWrapper.setStartTimeAmPm(timeStr[2].toLowerCase());
043 }
044
045 if (endDate !=null) {
046 String endDateFullString = formatter.format(endDate);
047 String[] timeStr = endDateFullString.split(" ");
048 timeSetWrapper.setEndDate(new SimpleDateFormat("MM/dd/yyyy").parse(timeStr[0]));
049 if (!"12:00".equals(timeStr[1])){
050 timeSetWrapper.setEndTime(timeStr[1]);
051 }
052 timeSetWrapper.setEndTimeAmPm(timeStr[2].toLowerCase());
053
054 }
055 }
056
057 public static boolean isValidDateRange(Date startDate,Date endDate){
058 if(startDate != null && endDate != null) {
059 if (startDate.after(endDate) || endDate.before(startDate)){
060 return false;
061 }
062 }
063 return true;
064 }
065
066 public static String formatDate(Date date){
067 return DateFormatUtils.format(date, CalendarConstants.DEFAULT_DATE_FORMAT);
068 }
069
070 public static boolean isDateWithinRange(Date startDate,Date endDate,Date checkDate){
071 if(startDate != null && endDate != null && checkDate != null) {
072 if (checkDate.before(startDate) || checkDate.after(endDate)){
073 return false;
074 }
075 }
076 return true;
077 }
078
079 public static RichTextInfo buildDesc(String descr){
080 RichTextInfo rti = new RichTextInfo();
081 rti.setPlain(descr);
082 return rti;
083 }
084
085 /* not currently used
086 public static Date getStartDate(TimeSetWrapper timeSetWrapper) throws Exception {
087 Date startDate = timeSetWrapper.getStartDate();
088 String startTime = timeSetWrapper.getStartTime();
089 String amPm = timeSetWrapper.getStartTimeAmPm();
090 Date fullStartDate = getDateWithTime(startDate, startTime, amPm);
091
092 return fullStartDate;
093 }
094 public static Date getEndDate(TimeSetWrapper timeSetWrapper) throws Exception {
095 Date endate = timeSetWrapper.getEndDate();
096 String endTime = timeSetWrapper.getEndTime();
097 String amPm = timeSetWrapper.getEndTimeAmPm();
098 Date fullEndDate = getDateWithTime(endate, endTime, amPm);
099
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 }