View Javadoc
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   * Created with IntelliJ IDEA.
16   * User: vivekb
17   * Date: 7/25/13
18   * Time: 5:20 PM
19   * To change this template use File | Settings | File Templates.
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); //minus number would decrement the days
29          return new Timestamp(cal.getTime().getTime());
30  
31  
32      }
33  
34      public static Timestamp addHours(Timestamp date, int numOfHours) {          //changed
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      }//end addHours method..
45  
46      // Replace with KK:mma if you want 0-11 interval
47      private static final DateFormat TWELVE_TF = new SimpleDateFormat("hh:mma");
48      // Replace with kk:mm if you want 1-24 interval
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  }