View Javadoc
1   /*
2    * Copyright 2011 The Kuali Foundation.
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.utility;
17  
18  import java.text.DecimalFormat;
19  import java.util.Calendar;
20  import java.util.Date;
21  
22  public class DateTimeUtil {
23  
24      public static String formatTime(long endTime, long startTime) {
25          long milliseconds = endTime - startTime;
26          return formatTime(milliseconds);
27      }
28  
29      public static String formatTime(long timeInMillisec) {
30          long hours;
31          long minutes;
32          long seconds;
33          long milliseconds = timeInMillisec;
34          double secAndMilliSec = (double) timeInMillisec / 1000;
35          double milliSec = secAndMilliSec - (long) secAndMilliSec;
36          DecimalFormat df = new DecimalFormat("#.###");
37          seconds = (milliseconds / 1000);
38          // milliseconds = milliseconds - seconds * 1000;
39          minutes = seconds / 60;
40          seconds = seconds - minutes * 60;
41          hours = minutes / 60;
42          minutes = minutes - hours * 60;
43          secAndMilliSec = (double) seconds + Double.valueOf(df.format(milliSec));
44          String timeTaken = hours + ":" + minutes + ":" + Double.valueOf(df.format(secAndMilliSec));
45          return timeTaken;
46      }
47  
48      public static Date formatDate(Date date){
49          Calendar calendar = Calendar.getInstance();
50          calendar.setTime(date);
51          return calendar.getTime();
52      }
53  
54      public static Date formateDateWithStartTimeOfTheDay(Date date){
55          Calendar calendar = Calendar.getInstance();
56          calendar.setTime(date);
57          calendar.set(Calendar.HOUR_OF_DAY, 00);
58          calendar.set(Calendar.MINUTE, 01);
59          calendar.set(Calendar.SECOND, 01);
60          calendar.set(Calendar.MILLISECOND, 01);
61          Date formatedDate = DateTimeUtil.formatDate(calendar.getTime());
62          return formatedDate;
63      }
64  
65      public static Date formateDateWithEndTimeOfTheDay(Date date){
66          Calendar calendar = Calendar.getInstance();
67          calendar.setTime(date);
68          calendar.set(Calendar.HOUR_OF_DAY, 23);
69          calendar.set(Calendar.MINUTE, 59);
70          calendar.set(Calendar.SECOND, 59);
71          calendar.set(Calendar.MILLISECOND, 59);
72          Date formatedDate = DateTimeUtil.formatDate(calendar.getTime());
73          return formatedDate;
74      }
75  
76  
77      
78  }