01: /**********************************************************************************
02: *
03: * $Id: ConversionUtil.java 9278 2006-05-10 23:29:21Z ray@media.berkeley.edu $
04: *
05: ***********************************************************************************
06: *
07: * Copyright (c) 2005 The Regents of the University of California, The Sakai Foundation.
08: *
09: * Licensed under the Educational Community License, Version 1.0 (the "License");
10: * you may not use this file except in compliance with the License.
11: * You may obtain a copy of the License at
12: *
13: * http://www.opensource.org/licenses/ecl1.php
14: *
15: * Unless required by applicable law or agreed to in writing, software
16: * distributed under the License is distributed on an "AS IS" BASIS,
17: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18: * See the License for the specific language governing permissions and
19: * limitations under the License.
20: *
21: **********************************************************************************/package org.sakaiproject.jsf.util;
22:
23: import java.sql.Time;
24: import java.util.Calendar;
25: import java.util.Date;
26: import java.util.GregorianCalendar;
27:
28: /**
29: * Provides methods helpful in making object conversions not provided for by
30: * the Sun or MyFaces distributions.
31: *
32: * @author <a href="mailto:jholtzman@berkeley.edu">Josh Holtzman</a>
33: *
34: */
35: public class ConversionUtil {
36:
37: /**
38: * The JSF DateTimeConverter can not convert into java.sql.Time (or into
39: * java.util.Calendar, for that matter!). So we do the conversion manually.
40: *
41: * @param date The date containing the time.
42: * @param am Whether this should be am (true) or pm (false)
43: * @return
44: */
45: public static Time convertDateToTime(Date date, boolean am) {
46: if (date == null) {
47: return null;
48: }
49:
50: Calendar cal = new GregorianCalendar();
51: cal.setTime(date);
52: int hourOfDay = cal.get(Calendar.HOUR_OF_DAY);
53:
54: if (am) {
55: // Check to make sure that the hours are indeed am hours
56: if (hourOfDay > 11) {
57: cal.set(Calendar.HOUR_OF_DAY, hourOfDay - 12);
58: date.setTime(cal.getTimeInMillis());
59: }
60: } else {
61: // Check to make sure that the hours are indeed pm hours
62: if (cal.get(Calendar.HOUR_OF_DAY) < 11) {
63: cal.set(Calendar.HOUR_OF_DAY, hourOfDay + 12);
64: date.setTime(cal.getTimeInMillis());
65: }
66: }
67: return new Time(date.getTime());
68: }
69:
70: }
71:
72: /**********************************************************************************
73: * $Id: ConversionUtil.java 9278 2006-05-10 23:29:21Z ray@media.berkeley.edu $
74: *********************************************************************************/
|