View Javadoc

1   /*
2    * $Id: Time.java,v 1.2 2007/05/24 12:26:17 oeuillot Exp $
3    */
4   package org.rcfaces.core.lang;
5   
6   import java.io.Serializable;
7   import java.util.Calendar;
8   import java.util.Date;
9   import java.util.Locale;
10  import java.util.TimeZone;
11  
12  import javax.faces.context.FacesContext;
13  
14  /**
15   * 
16   * @author Olivier Oeuillot (latest modification by $Author: oeuillot $)
17   * @version $Revision: 1.2 $ $Date: 2007/05/24 12:26:17 $
18   */
19  public class Time extends DefaultAdaptable implements Serializable, Comparable {
20      private static final String REVISION = "$Revision: 1.2 $";
21  
22      private static final long serialVersionUID = -5495525689862764775L;
23  
24      private int time;
25  
26      public Time() {
27          this(0, 0, 0, 0);
28      }
29  
30      public Time(int hours) {
31          this(hours, 0, 0, 0);
32      }
33  
34      public Time(int hours, int minutes) {
35          this(hours, minutes, 0, 0);
36      }
37  
38      public Time(int hours, int minutes, int seconds) {
39          this(hours, minutes, seconds, 0);
40      }
41  
42      public Time(int hours, int minutes, int seconds, int millis) {
43          this.time = ((((hours * 60) + minutes) * 60) + seconds) * 1000 + millis;
44      }
45  
46      public Time(Date d, TimeZone timeZone) {
47          this(d, Calendar.getInstance(timeZone));
48      }
49  
50      public Time(Date d, Locale locale) {
51          this(d, Calendar.getInstance(locale));
52      }
53  
54      public Time(Date d, Calendar calendar) {
55          int hours;
56          int minutes;
57          int seconds;
58          int millis;
59  
60          synchronized (calendar) {
61              calendar.setTime(d);
62  
63              hours = calendar.get(Calendar.HOUR_OF_DAY);
64              minutes = calendar.get(Calendar.MINUTE);
65              seconds = calendar.get(Calendar.SECOND);
66              millis = calendar.get(Calendar.MILLISECOND);
67          }
68  
69          this.time = ((((hours * 60) + minutes) * 60) + seconds) * 1000 + millis;
70      }
71  
72      public int getHours() {
73          return time / (60 * 60 * 1000);
74      }
75  
76      public int getMinutes() {
77          return (time / (60 * 1000)) % 60;
78      }
79  
80      public int getSeconds() {
81          return (time / 1000) % 60;
82      }
83  
84      public int getMillis() {
85          return time % 1000;
86      }
87  
88      public int compareTo(Object o) {
89          return time - ((Time) o).time;
90      }
91  
92      public int hashCode() {
93          return time;
94      }
95  
96      public long getTime() {
97          return time;
98      }
99  
100     public Date getDate(TimeZone timeZone) {
101         Calendar calendar = Calendar.getInstance(timeZone);
102 
103         return getDate(calendar);
104     }
105 
106     public Date getDate(Locale locale) {
107         if (locale == null) {
108             locale = FacesContext.getCurrentInstance().getViewRoot()
109                     .getLocale();
110         }
111         Calendar calendar = Calendar.getInstance(locale);
112 
113         return getDate(calendar);
114     }
115 
116     public Date getDate(Calendar calendar) {
117         calendar.set(Calendar.HOUR_OF_DAY, getHours());
118         calendar.set(Calendar.MINUTE, getMinutes());
119         calendar.set(Calendar.SECOND, getSeconds());
120         calendar.set(Calendar.MILLISECOND, getMillis());
121 
122         return calendar.getTime();
123     }
124 
125     public boolean equals(Object obj) {
126         if (this == obj)
127             return true;
128         if (obj == null)
129             return false;
130         if (getClass() != obj.getClass())
131             return false;
132         final Time other = (Time) obj;
133         if (time != other.time)
134             return false;
135         return true;
136     }
137 
138     public String toString() {
139         return "[Time " + getHours() + ":" + getMinutes() + ":" + getSeconds()
140                 + "." + getMillis() + "]";
141     }
142 }