View Javadoc
1   package org.kuali.ole.deliver.calendar.service;
2   
3   import java.sql.Timestamp;
4   import java.text.DateFormat;
5   import java.text.ParseException;
6   import java.text.SimpleDateFormat;
7   import java.util.Calendar;
8   
9   
10  /**
11   * Created with IntelliJ IDEA.
12   * User: vivekb
13   * Date: 7/25/13
14   * Time: 5:20 PM
15   * To change this template use File | Settings | File Templates.
16   */
17  public class DateUtil {
18      public static Timestamp addDays(Timestamp date, int days) {
19          Calendar cal = Calendar.getInstance();
20          cal.setTime(date);
21          cal.add(Calendar.DATE, days); //minus number would decrement the days
22          return new Timestamp(cal.getTime().getTime());
23  
24  
25      }
26  
27      public static Timestamp addHours(Timestamp date, int numOfHours) {          //changed
28  
29          Long milliSecInAnHour = new Long(60 * 60 * 1000);
30          Timestamp newTS = new Timestamp(date.getTime());
31          long milliSecToAdd = milliSecInAnHour * numOfHours;
32          long newTimeMilliSec = newTS.getTime();
33          newTS.setTime(newTimeMilliSec + milliSecToAdd);
34          return newTS;
35  
36  
37      }//end addHours method..
38  
39      // Replace with KK:mma if you want 0-11 interval
40      private static final DateFormat TWELVE_TF = new SimpleDateFormat("hh:mma");
41      // Replace with kk:mm if you want 1-24 interval
42      private static final DateFormat TWENTY_FOUR_TF = new SimpleDateFormat("HH:mm");
43  
44      public static String convertTo24HoursFormat(String twelveHourTime)
45              throws ParseException {
46          return TWENTY_FOUR_TF.format(
47                  TWELVE_TF.parse(twelveHourTime));
48      }
49  
50      public static String convertTo12HoursFormat(String twentyFourHourTime)
51              throws ParseException {
52          return TWELVE_TF.format(
53                  TWENTY_FOUR_TF.parse(twentyFourHourTime));
54      }
55  
56  
57  }