1 package org.kuali.ole.deliver.calendar.service;
2
3 import freemarker.template.SimpleDate;
4 import org.kuali.rice.core.api.util.RiceConstants;
5
6 import java.sql.Timestamp;
7 import java.text.DateFormat;
8 import java.text.ParseException;
9 import java.text.SimpleDateFormat;
10 import java.util.Calendar;
11 import java.util.Date;
12
13
14
15
16
17
18
19
20
21 public class DateUtil {
22
23 private SimpleDateFormat simpleDateFormat;
24
25 public static Timestamp addDays(Timestamp date, int days) {
26 Calendar cal = Calendar.getInstance();
27 cal.setTime(date);
28 cal.add(Calendar.DATE, days);
29 return new Timestamp(cal.getTime().getTime());
30
31
32 }
33
34 public static Timestamp addHours(Timestamp date, int numOfHours) {
35
36 Long milliSecInAnHour = new Long(60 * 60 * 1000);
37 Timestamp newTS = new Timestamp(date.getTime());
38 long milliSecToAdd = milliSecInAnHour * numOfHours;
39 long newTimeMilliSec = newTS.getTime();
40 newTS.setTime(newTimeMilliSec + milliSecToAdd);
41 return newTS;
42
43
44 }
45
46
47 private static final DateFormat TWELVE_TF = new SimpleDateFormat("hh:mma");
48
49 private static final DateFormat TWENTY_FOUR_TF = new SimpleDateFormat("HH:mm");
50
51 public static String convertTo24HoursFormat(String twelveHourTime)
52 throws ParseException {
53 return TWENTY_FOUR_TF.format(
54 TWELVE_TF.parse(twelveHourTime));
55 }
56
57 public static String convertTo12HoursFormat(String twentyFourHourTime)
58 throws ParseException {
59 return TWELVE_TF.format(
60 TWENTY_FOUR_TF.parse(twentyFourHourTime));
61 }
62
63
64 public Date getDate(String dateString) {
65 SimpleDateFormat dateFormat = getDateFormat();
66 try {
67 return dateFormat.parse(dateString);
68 } catch (ParseException e) {
69 e.printStackTrace();
70 }
71 return null;
72 }
73
74 private SimpleDateFormat getDateFormat() {
75 if(simpleDateFormat == null){
76 simpleDateFormat = new SimpleDateFormat(RiceConstants.SIMPLE_DATE_FORMAT_FOR_DATE);
77 }
78 return simpleDateFormat;
79 }
80
81 public void setSimpleDateFormat(SimpleDateFormat simpleDateFormat) {
82 this.simpleDateFormat = simpleDateFormat;
83 }
84 }