blob: 357eaae599d1b9aafebdced7000a8168c0b4b2c6 [file] [log] [blame]
Francesco Furfariec7e1752006-10-02 13:37:04 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
Francesco Furfarid1072b52006-05-03 07:44:48 +00009 *
Francesco Furfariec7e1752006-10-02 13:37:04 +000010 * http://www.apache.org/licenses/LICENSE-2.0
Francesco Furfarid1072b52006-05-03 07:44:48 +000011 *
Francesco Furfariec7e1752006-10-02 13:37:04 +000012 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
Francesco Furfarid1072b52006-05-03 07:44:48 +000018 */
Francesco Furfariec7e1752006-10-02 13:37:04 +000019
Francesco Furfarid1072b52006-05-03 07:44:48 +000020package org.apache.felix.upnp.sample.clock;
21
22import java.util.Calendar;
23/*
Karl Paulsd312acc2007-06-18 20:38:33 +000024* @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
Francesco Furfarid1072b52006-05-03 07:44:48 +000025*/
26
27public class Clock
28{
29 private Calendar cal;
30
31 public Clock(Calendar cal)
32 {
33 this.cal = cal;
34 }
35
36 public Calendar getCalendar()
37 {
38 return cal;
39 }
40
41 ////////////////////////////////////////////////
42 // Time
43 ////////////////////////////////////////////////
44
45 public int getHour()
46 {
47 return getCalendar().get(Calendar.HOUR);
48 }
49
50 public int getMinute()
51 {
52 return getCalendar().get(Calendar.MINUTE);
53 }
54
55 public int getSecond()
56 {
57 return getCalendar().get(Calendar.SECOND);
58 }
59
60 ////////////////////////////////////////////////
61 // paint
62 ////////////////////////////////////////////////
63
64 public final static Clock getInstance()
65 {
66 return new Clock(Calendar.getInstance());
67 }
68
69 ////////////////////////////////////////////////
70 // getDateString
71 ////////////////////////////////////////////////
72
73 public final static String toClockString(int value)
74 {
75 if (value < 10)
76 return "0" + Integer.toString(value);
77 return Integer.toString(value);
78 }
79
80 private final static String MONTH_STRING[] = {
81 "Jan",
82 "Feb",
83 "Mar",
84 "Apr",
85 "May",
86 "Jun",
87 "Jul",
88 "Aug",
89 "Sep",
90 "Oct",
91 "Nov",
92 "Dec",
93 };
94
95 public final static String toMonthString(int value)
96 {
97 value -= Calendar.JANUARY;
98 if (0 <= value && value < 12)
99 return MONTH_STRING[value];
100 return "";
101 }
102
103 private final static String WEEK_STRING[] = {
104 "Sun",
105 "Mon",
106 "Tue",
107 "Wed",
108 "Thu",
109 "Fri",
110 "Sat",
111 };
112
113 public final static String toWeekString(int value)
114 {
115 value -= Calendar.SUNDAY;
116 if (0 <= value && value < 7)
117 return WEEK_STRING[value];
118 return "";
119 }
120
121 public String getDateString()
122 {
123 Calendar cal = getCalendar();
124 return
125 toWeekString(cal.get(Calendar.DAY_OF_WEEK)) +", " +
126 toMonthString(cal.get(Calendar.MONTH)) + " " +
127 Integer.toString(cal.get(Calendar.DATE)) + ", " +
128 toClockString(cal.get(Calendar.YEAR) % 100);
129 }
130
131 ////////////////////////////////////////////////
132 // getTimeString
133 ////////////////////////////////////////////////
134
135 public String getTimeString()
136 {
137 Calendar cal = getCalendar();
138 return
139 toClockString(cal.get(Calendar.HOUR)) +
140 (((cal.get(Calendar.SECOND) % 2) == 0) ? ":" : " ") +
141 toClockString(cal.get(Calendar.MINUTE));
142 }
143
144 ////////////////////////////////////////////////
145 // toString
146 ////////////////////////////////////////////////
147
148 public String toString()
149 {
150 Calendar cal = getCalendar();
151 return
152 getDateString() + ", " +
153 toClockString(cal.get(Calendar.HOUR)) + ":" +
154 toClockString(cal.get(Calendar.MINUTE)) + ":" +
155 toClockString(cal.get(Calendar.SECOND));
156 }
157
158}
159