Develop and Download Open Source Software

Browse CVS Repository

Contents of /pal/libraries/common-utils/src/main/java/jp/sf/pal/common/util/DateUtil.java

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Thu Jan 25 22:41:33 2007 UTC (17 years, 1 month ago) by shinsuke
Branch: MAIN
CVS Tags: HEAD
File MIME type: text/x-java
added common-util library

1 /*
2 * Copyright 2004-2007 The Portal Application Laboratory Team.
3 *
4 * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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,
13 * either express or implied. See the License for the specific language
14 * governing permissions and limitations under the License.
15 */
16 package jp.sf.pal.common.util;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20
21 import java.util.Calendar;
22 import java.util.Date;
23
24 public class DateUtil {
25 /**
26 * Logger for this class
27 */
28 private static final Log log = LogFactory.getLog(DateUtil.class);
29
30 /**
31 * Allocates a Date object and initializes it so that it represents
32 * midnight, local time, at the beginning of the day specified by the year,
33 * month, and date arguments.
34 *
35 * @param year
36 * @param month
37 * @param date
38 * @return
39 */
40 public static Date get(int year, int month, int date) {
41 if (log.isDebugEnabled()) {
42 log.debug("get(int, int, int) - : year=" + year + ", month="
43 + month + ", date=" + date);
44 }
45
46 Calendar cal = Calendar.getInstance();
47 cal.set(year + 1900, month, date);
48 return cal.getTime();
49 }
50
51 /**
52 * Allocates a Date object and initializes it so that it represents the
53 * instant at the start of the minute specified by the year, month, date,
54 * hrs, and min arguments, in the local time zone.
55 *
56 * @param year
57 * @param month
58 * @param date
59 * @param hrs
60 * @param min
61 * @return
62 */
63 public static Date get(int year, int month, int date, int hrs, int min) {
64 if (log.isDebugEnabled()) {
65 log.debug("get(int, int, int, int, int) - : year=" + year
66 + ", month=" + month + ", date=" + date + ", hrs=" + hrs
67 + ", min=" + min);
68 }
69
70 Calendar cal = Calendar.getInstance();
71 cal.set(year + 1900, month, date, hrs, min);
72 return cal.getTime();
73 }
74
75 /**
76 * Allocates a Date object and initializes it so that it represents the
77 * instant at the start of the second specified by the year, month, date,
78 * hrs, min, and sec arguments, in the local time zone.
79 *
80 * @param year
81 * @param month
82 * @param date
83 * @param hrs
84 * @param min
85 * @param sec
86 * @return
87 */
88 public static Date get(int year, int month, int date, int hrs, int min,
89 int sec) {
90 if (log.isDebugEnabled()) {
91 log.debug("get(int, int, int, int, int, int) - : year=" + year
92 + ", month=" + month + ", date=" + date + ", hrs=" + hrs
93 + ", min=" + min + ", sec=" + sec);
94 }
95
96 Calendar cal = Calendar.getInstance();
97 cal.set(year + 1900, month, date, hrs, min, sec);
98 return cal.getTime();
99
100 }
101
102 /**
103 * Allocates a Date object and initializes it so that it represents the
104 * instant at the start of the second specified by the year, month, date,
105 * hrs, min, sec and millisec arguments, in the local time zone.
106 *
107 * @param year
108 * @param month
109 * @param date
110 * @param hrs
111 * @param min
112 * @param sec
113 * @param millisec
114 * @return
115 */
116 public static Date get(int year, int month, int date, int hrs, int min,
117 int sec, int millisec) {
118 if (log.isDebugEnabled()) {
119 log.debug("get(int, int, int, int, int, int) - : year=" + year
120 + ", month=" + month + ", date=" + date + ", hrs=" + hrs
121 + ", min=" + min + ", sec=" + sec + ", millisec="
122 + millisec);
123 }
124
125 Calendar cal = Calendar.getInstance();
126 cal.set(year + 1900, month, date, hrs, min, sec);
127 cal.set(Calendar.MILLISECOND, millisec);
128 return cal.getTime();
129
130 }
131
132 /**
133 * Returns a value that is the result of subtracting 1900 from the year that
134 * contains or begins with the instant in time represented by this Date
135 * object, as interpreted in the local time zone.
136 *
137 * @param d
138 * @return
139 */
140 public static int getYear(Date d) {
141 Calendar cal = Calendar.getInstance();
142 cal.setTime(d);
143 return cal.get(Calendar.YEAR) - 1900;
144 }
145
146 /**
147 * Returns a number representing the month that contains or begins with the
148 * instant in time represented by this Date object. The value returned is
149 * between 0 and 11, with the value 0 representing January.
150 *
151 * @param d
152 * @return
153 */
154 public static int getMonth(Date d) {
155 Calendar cal = Calendar.getInstance();
156 cal.setTime(d);
157 return cal.get(Calendar.MONTH);
158 }
159
160 /**
161 * Returns the day of the month represented by this Date object. The value
162 * returned is between 1 and 31 representing the day of the month that
163 * contains or begins with the instant in time represented by this Date
164 * object, as interpreted in the local time zone.
165 *
166 * @param d
167 * @return
168 */
169 public static int getDate(Date d) {
170 Calendar cal = Calendar.getInstance();
171 cal.setTime(d);
172 return cal.get(Calendar.DAY_OF_MONTH);
173 }
174
175 /**
176 * Returns the day of the week represented by this date. The returned value
177 * (0 = Sunday, 1 = Monday, 2 = Tuesday, 3 = Wednesday, 4 = Thursday, 5 =
178 * Friday, 6 = Saturday) represents the day of the week that contains or
179 * begins with the instant in time represented by this Date object, as
180 * interpreted in the local time zone.
181 *
182 * @param d
183 * @return
184 */
185 public static int getDay(Date d) {
186 Calendar cal = Calendar.getInstance();
187 cal.setTime(d);
188 return cal.get(Calendar.DAY_OF_WEEK) - 1;
189 }
190
191 /**
192 * Returns the hour represented by this Date object. The returned value is a
193 * number (0 through 23) representing the hour within the day that contains
194 * or begins with the instant in time represented by this Date object, as
195 * interpreted in the local time zone.
196 *
197 * @param d
198 * @return
199 */
200 public static int getHours(Date d) {
201 Calendar cal = Calendar.getInstance();
202 cal.setTime(d);
203 return cal.get(Calendar.HOUR_OF_DAY);
204 }
205
206 /**
207 * Returns the number of minutes past the hour represented by this date, as
208 * interpreted in the local time zone. The value returned is between 0 and
209 * 59.
210 *
211 * @param d
212 * @return
213 */
214 public static int getMinutes(Date d) {
215 Calendar cal = Calendar.getInstance();
216 cal.setTime(d);
217 return cal.get(Calendar.MINUTE);
218 }
219
220 /**
221 * Returns the number of seconds past the minute represented by this date.
222 * The value returned is between 0 and 61. The values 60 and 61 can only
223 * occur on those Java Virtual Machines that take leap seconds into account.
224 *
225 * @param d
226 * @return
227 */
228 public static int getSeconds(Date d) {
229 Calendar cal = Calendar.getInstance();
230 cal.setTime(d);
231 return cal.get(Calendar.SECOND);
232 }
233
234 }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26