java日期时间处理工具类封装
在日常的开发中经常会遇到时间的处理。比如格式化后打印出来,得到明天的日期等等。java中也提供了很多类来处理时间,比如Calendar,java.util.Date,java.sql.Timestamp,SimpleDateFormat等等。昨天晚上写了一个工具类,封装了一些常用的方法。比如拿到当前的年月日时分秒,昨天,明天,之前n天,之后n天,格式化,还有各种时间类型之间的互相转换等等。如果还有什么需要的话,请告诉我,我好完善它。谢谢。
在最后提供了完整代码的下载,方便大家查看和使用。
时间工具类
1 package com.haode.bugtrace.util;
2
3 import java.sql.Time;
4 import java.sql.Timestamp;
5 import java.text.SimpleDateFormat;
6 import java.util.Calendar;
7 import java.util.Date;
8
9 /**
10 * 日期时间处理工具类
11 * @author laichendong
12 * @since 2010年2月22日23:09:09
13 *
14 */
15 public class DateTime {
16
17 private long lNow = System.currentTimeMillis();
18 private Calendar cNow = Calendar.getInstance();
19 private Date dNow = new Date(lNow);
20 private Timestamp tNow = new Timestamp(lNow);
21 private java.sql.Date today = new java.sql.Date(lNow);
22 private java.sql.Time now = new java.sql.Time(lNow);
23
24 /**
25 * 默认构造方法
26 */
27 public DateTime() {
28
29 }
30
31 /*private DateTime(long lNow, Calendar cNow, Date dNow, Timestamp tNow,
32 java.sql.Date today, Time now) {
33 this.lNow = lNow;
34 this.cNow = cNow;
35 this.dNow = dNow;
36 this.tNow = tNow;
37 this.today = today;
38 this.now = now;
39 }*/
40
41 /**
42 * 得到年
43 * @param c
44 * @return
45 */
46 public static int getYear(Calendar c){
47 if(c != null){
48 return c.get(Calendar.YEAR);
49 }else{
50 return Calendar.getInstance().get(Calendar.YEAR);
51 }
52 }
53
54 /**
55 * 得到月份
56 * 注意,这里的月份依然是从0开始的
57 * @param c
58 * @return
59 */
60 public static int getMonth(Calendar c){
61 if(c != null){
62 return c.get(Calendar.MONTH);
63 }else{
64 return Calendar.getInstance().get(Calendar.MONTH);
65 }
66 }
67
68 /**
69 * 得到日期
70 * @param c
71 * @return
72 */
73 public static int getDate(Calendar c){
74 if(c != null){
75 return c.get(Calendar.DATE);
76 }else{
77 return Calendar.getInstance().get(Calendar.DATE);
78 }
79 }
80
81 /**
82 * 得到星期
83 * @param c
84 * @return
85 */
86 public static int getDay(Calendar c){
87 if(c != null){
88 return c.get(Calendar.DAY_OF_WEEK);
89 }else{
90 return Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
91 }
92 }
93
94 /**
95 * 得到小时
96 * @param c
97 * @return
98 */
99 public static int getHour(Calendar c){
100 if(c != null){
101 return c.get(Calendar.HOUR);
102 }else{
103 return Calendar.getInstance().get(Calendar.HOUR);
104 }
105 }
106
107 /**
108 * 得到分钟
109 * @param c
110 * @return
111 */
112 public static int getMinute(Calendar c){
113 if(c != null){
114 return c.get(Calendar.MINUTE);
115 }else{
116 return Calendar.getInstance().get(Calendar.MINUTE);
117 }
118 }
119
120 /**
121 * 得到秒
122 * @param c
123 * @return
124 */
125 public static int getSecond(Calendar c){
126 if(c != null){
127 return c.get(Calendar.SECOND);
128 }else{
129 return Calendar.getInstance().get(Calendar.SECOND);
130 }
131 }
132
133 /**
134 * 得到指定或者当前时间前n天的Calendar
135 * @param c
136 * @param n
137 * @return
138 */
139 public static Calendar beforeNDays(Calendar c, int n){
140 //偏移量,给定n天的毫秒数
141 long offset = n*24*60*60*1000;
142 Calendar calendar = null;
143 if(c != null){
144 calendar = c;
145 }else{
146 calendar = Calendar.getInstance();
147 }
148
149 calendar.setTimeInMillis(calendar.getTimeInMillis() - offset);
150 return calendar;
151 }
152
153 /**
154 * 得到指定或者当前时间后n天的Calendar
155 * @param c
156 * @param n
157 * @return
158 */
159 public static Calendar afterNDays(Calendar c, int n){
160 //偏移量,给定n天的毫秒数
161 long offset = n*24*60*60*1000;
162 Calendar calendar = null;
163 if(c != null){
164 calendar = c;
165 }else{
166 calendar = Calendar.getInstance();
167 }
168
169 calendar.setTimeInMillis(calendar.getTimeInMillis() + offset);
170 return calendar;
171 }
172
173 /**
174 * 昨天
175 * @param c
176 * @return
177 */
178 public static Calendar yesterday(Calendar c){
179 long offset = 1*24*60*60*1000;
180 Calendar calendar = null;
181 if(c != null){
182 calendar = c;
183 }else{
184 calendar = Calendar.getInstance();
185 }
186
187 calendar.setTimeInMillis(calendar.getTimeInMillis() - offset);
188 return calendar;
189 }
190
191 /**
192 * 明天
193 * @param c
194 * @return
195 */
196 public static Calendar tomorrow(Calendar c){
197 long offset = 1*24*60*60*1000;
198 Calendar calendar = null;
199 if(c != null){
200 calendar = c;
201 }else{
202 calendar = Calendar.getInstance();
203 }
204
205 calendar.setTimeInMillis(calendar.getTimeInMillis() + offset);
206 return calendar;
207 }
208
209 /**
210 * 得到指定或者当前时间前offset毫秒的Calendar
211 * @param c
212 * @param offset
213 * @return
214 */
215 public static Calendar before(Calendar c, long offset){
216 Calendar calendar = null;
217 if(c != null){
218 calendar = c;
219 }else{
220 calendar = Calendar.getInstance();
221 }
222
223 calendar.setTimeInMillis(calendar.getTimeInMillis() - offset);
224 return calendar;
225 }
226
227 /**
228 * 得到指定或者当前时间前offset毫秒的Calendar
229 * @param c
230 * @param offset
231 * @return
232 */
233 public static Calendar after(Calendar c, long offset){
234 Calendar calendar = null;
235 if(c != null){
236 calendar = c;
237 }else{
238 calendar = Calendar.getInstance();
239 }
240
241 calendar.setTimeInMillis(calendar.getTimeInMillis() - offset);
242 return calendar;
243 }
244
245 /**
246 * 日期格式化
247 * @param c
248 * @param pattern
249 * @return
250 */
251 public static String format(Calendar c, String pattern){
252 Calendar calendar = null;
253 if(c != null){
254 calendar = c;
255 }else{
256 calendar = Calendar.getInstance();
257 }
258 if(pattern == null || pattern.equals("")){
259 pattern = "yyyy年MM月dd日 HH:mm:ss";
260 }
261 SimpleDateFormat sdf = new SimpleDateFormat(pattern);
262
263 return sdf.format(calendar.getTime());
264 }
265
266 /**
267 * Date类型转换到Calendar类型
268 * @param d
269 * @return
270 */
271 public static Calendar Date2Calendar(Date d){
272 Calendar c = Calendar.getInstance();
273 c.setTime(d);
274 return c;
275 }
276
277 /**
278 * Calendar类型转换到Date类型
279 * @param c
280 * @return
281 */
282 public static Date Calendar2Date(Calendar c){
283 return c.getTime();
284 }
285
286 /**
287 * Date类型转换到Timestamp类型
288 * @param d
289 * @return
290 */
291 public static Timestamp Date2Timestamp(Date d){
292 return new Timestamp(d.getTime());
293 }
294
295 /**
296 * Calendar类型转换到Timestamp类型
297 * @param c
298 * @return
299 */
300 public static Timestamp Calendar2Timestamp(Calendar c){
301 return new Timestamp(c.getTimeInMillis());
302 }
303
304 /**
305 * Timestamp类型转换到Calendar类型
306 * @param ts
307 * @return
308 */
309 public static Calendar Timestamp2Calendar(Timestamp ts){
310 Calendar c = Calendar.getInstance();
311 c.setTime(ts);
312 return c;
313 }
314
315 /**
316 * 得到当前时间的字符串表示
317 * 格式2010-02-02 12:12:12
318 * @return
319 */
320 public static String getTimeString(){
321 return format(Calendar.getInstance(), "yyyy-MM-dd HH:mm:ss");
322 }
323
324 /**
325 * 标准日期格式字符串解析成Calendar对象
326 * @param s
327 * @return
328 */
329 public static Calendar pars2Calender(String s){
330 Timestamp ts = Timestamp.valueOf(s);
331 return Timestamp2Calendar(ts);
332 }
333
334 //================以下是get和set方法=========================//
335
336 public long getLNow() {
337 return lNow;
338 }
339
340 public void setLNow(long now) {
341 lNow = now;
342 }
343
344 public Calendar getCNow() {
345 return cNow;
346 }
347
348 public void setCNow(Calendar now) {
349 cNow = now;
350 }
351
352 public Date getDNow() {
353 return dNow;
354 }
355
356 public void setDNow(Date now) {
357 dNow = now;
358 }
359
360 public Timestamp getTNow() {
361 return tNow;
362 }
363
364 public void setTNow(Timestamp now) {
365 tNow = now;
366 }
367
368 public java.sql.Date getToday() {
369 return today;
370 }
371
372 public void setToday(java.sql.Date today) {
373 this.today = today;
374 }
375
376 public java.sql.Time getNow() {
377 return now;
378 }
379
380 public void setNow(java.sql.Time now) {
381 this.now = now;
382 }
383
384 /**
385 * @param args
386 */
387 public static void main(String[] args) {
388 // TODO Auto-generated method stub
389
390 }
391
392 }
393
2
3 import java.sql.Time;
4 import java.sql.Timestamp;
5 import java.text.SimpleDateFormat;
6 import java.util.Calendar;
7 import java.util.Date;
8
9 /**
10 * 日期时间处理工具类
11 * @author laichendong
12 * @since 2010年2月22日23:09:09
13 *
14 */
15 public class DateTime {
16
17 private long lNow = System.currentTimeMillis();
18 private Calendar cNow = Calendar.getInstance();
19 private Date dNow = new Date(lNow);
20 private Timestamp tNow = new Timestamp(lNow);
21 private java.sql.Date today = new java.sql.Date(lNow);
22 private java.sql.Time now = new java.sql.Time(lNow);
23
24 /**
25 * 默认构造方法
26 */
27 public DateTime() {
28
29 }
30
31 /*private DateTime(long lNow, Calendar cNow, Date dNow, Timestamp tNow,
32 java.sql.Date today, Time now) {
33 this.lNow = lNow;
34 this.cNow = cNow;
35 this.dNow = dNow;
36 this.tNow = tNow;
37 this.today = today;
38 this.now = now;
39 }*/
40
41 /**
42 * 得到年
43 * @param c
44 * @return
45 */
46 public static int getYear(Calendar c){
47 if(c != null){
48 return c.get(Calendar.YEAR);
49 }else{
50 return Calendar.getInstance().get(Calendar.YEAR);
51 }
52 }
53
54 /**
55 * 得到月份
56 * 注意,这里的月份依然是从0开始的
57 * @param c
58 * @return
59 */
60 public static int getMonth(Calendar c){
61 if(c != null){
62 return c.get(Calendar.MONTH);
63 }else{
64 return Calendar.getInstance().get(Calendar.MONTH);
65 }
66 }
67
68 /**
69 * 得到日期
70 * @param c
71 * @return
72 */
73 public static int getDate(Calendar c){
74 if(c != null){
75 return c.get(Calendar.DATE);
76 }else{
77 return Calendar.getInstance().get(Calendar.DATE);
78 }
79 }
80
81 /**
82 * 得到星期
83 * @param c
84 * @return
85 */
86 public static int getDay(Calendar c){
87 if(c != null){
88 return c.get(Calendar.DAY_OF_WEEK);
89 }else{
90 return Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
91 }
92 }
93
94 /**
95 * 得到小时
96 * @param c
97 * @return
98 */
99 public static int getHour(Calendar c){
100 if(c != null){
101 return c.get(Calendar.HOUR);
102 }else{
103 return Calendar.getInstance().get(Calendar.HOUR);
104 }
105 }
106
107 /**
108 * 得到分钟
109 * @param c
110 * @return
111 */
112 public static int getMinute(Calendar c){
113 if(c != null){
114 return c.get(Calendar.MINUTE);
115 }else{
116 return Calendar.getInstance().get(Calendar.MINUTE);
117 }
118 }
119
120 /**
121 * 得到秒
122 * @param c
123 * @return
124 */
125 public static int getSecond(Calendar c){
126 if(c != null){
127 return c.get(Calendar.SECOND);
128 }else{
129 return Calendar.getInstance().get(Calendar.SECOND);
130 }
131 }
132
133 /**
134 * 得到指定或者当前时间前n天的Calendar
135 * @param c
136 * @param n
137 * @return
138 */
139 public static Calendar beforeNDays(Calendar c, int n){
140 //偏移量,给定n天的毫秒数
141 long offset = n*24*60*60*1000;
142 Calendar calendar = null;
143 if(c != null){
144 calendar = c;
145 }else{
146 calendar = Calendar.getInstance();
147 }
148
149 calendar.setTimeInMillis(calendar.getTimeInMillis() - offset);
150 return calendar;
151 }
152
153 /**
154 * 得到指定或者当前时间后n天的Calendar
155 * @param c
156 * @param n
157 * @return
158 */
159 public static Calendar afterNDays(Calendar c, int n){
160 //偏移量,给定n天的毫秒数
161 long offset = n*24*60*60*1000;
162 Calendar calendar = null;
163 if(c != null){
164 calendar = c;
165 }else{
166 calendar = Calendar.getInstance();
167 }
168
169 calendar.setTimeInMillis(calendar.getTimeInMillis() + offset);
170 return calendar;
171 }
172
173 /**
174 * 昨天
175 * @param c
176 * @return
177 */
178 public static Calendar yesterday(Calendar c){
179 long offset = 1*24*60*60*1000;
180 Calendar calendar = null;
181 if(c != null){
182 calendar = c;
183 }else{
184 calendar = Calendar.getInstance();
185 }
186
187 calendar.setTimeInMillis(calendar.getTimeInMillis() - offset);
188 return calendar;
189 }
190
191 /**
192 * 明天
193 * @param c
194 * @return
195 */
196 public static Calendar tomorrow(Calendar c){
197 long offset = 1*24*60*60*1000;
198 Calendar calendar = null;
199 if(c != null){
200 calendar = c;
201 }else{
202 calendar = Calendar.getInstance();
203 }
204
205 calendar.setTimeInMillis(calendar.getTimeInMillis() + offset);
206 return calendar;
207 }
208
209 /**
210 * 得到指定或者当前时间前offset毫秒的Calendar
211 * @param c
212 * @param offset
213 * @return
214 */
215 public static Calendar before(Calendar c, long offset){
216 Calendar calendar = null;
217 if(c != null){
218 calendar = c;
219 }else{
220 calendar = Calendar.getInstance();
221 }
222
223 calendar.setTimeInMillis(calendar.getTimeInMillis() - offset);
224 return calendar;
225 }
226
227 /**
228 * 得到指定或者当前时间前offset毫秒的Calendar
229 * @param c
230 * @param offset
231 * @return
232 */
233 public static Calendar after(Calendar c, long offset){
234 Calendar calendar = null;
235 if(c != null){
236 calendar = c;
237 }else{
238 calendar = Calendar.getInstance();
239 }
240
241 calendar.setTimeInMillis(calendar.getTimeInMillis() - offset);
242 return calendar;
243 }
244
245 /**
246 * 日期格式化
247 * @param c
248 * @param pattern
249 * @return
250 */
251 public static String format(Calendar c, String pattern){
252 Calendar calendar = null;
253 if(c != null){
254 calendar = c;
255 }else{
256 calendar = Calendar.getInstance();
257 }
258 if(pattern == null || pattern.equals("")){
259 pattern = "yyyy年MM月dd日 HH:mm:ss";
260 }
261 SimpleDateFormat sdf = new SimpleDateFormat(pattern);
262
263 return sdf.format(calendar.getTime());
264 }
265
266 /**
267 * Date类型转换到Calendar类型
268 * @param d
269 * @return
270 */
271 public static Calendar Date2Calendar(Date d){
272 Calendar c = Calendar.getInstance();
273 c.setTime(d);
274 return c;
275 }
276
277 /**
278 * Calendar类型转换到Date类型
279 * @param c
280 * @return
281 */
282 public static Date Calendar2Date(Calendar c){
283 return c.getTime();
284 }
285
286 /**
287 * Date类型转换到Timestamp类型
288 * @param d
289 * @return
290 */
291 public static Timestamp Date2Timestamp(Date d){
292 return new Timestamp(d.getTime());
293 }
294
295 /**
296 * Calendar类型转换到Timestamp类型
297 * @param c
298 * @return
299 */
300 public static Timestamp Calendar2Timestamp(Calendar c){
301 return new Timestamp(c.getTimeInMillis());
302 }
303
304 /**
305 * Timestamp类型转换到Calendar类型
306 * @param ts
307 * @return
308 */
309 public static Calendar Timestamp2Calendar(Timestamp ts){
310 Calendar c = Calendar.getInstance();
311 c.setTime(ts);
312 return c;
313 }
314
315 /**
316 * 得到当前时间的字符串表示
317 * 格式2010-02-02 12:12:12
318 * @return
319 */
320 public static String getTimeString(){
321 return format(Calendar.getInstance(), "yyyy-MM-dd HH:mm:ss");
322 }
323
324 /**
325 * 标准日期格式字符串解析成Calendar对象
326 * @param s
327 * @return
328 */
329 public static Calendar pars2Calender(String s){
330 Timestamp ts = Timestamp.valueOf(s);
331 return Timestamp2Calendar(ts);
332 }
333
334 //================以下是get和set方法=========================//
335
336 public long getLNow() {
337 return lNow;
338 }
339
340 public void setLNow(long now) {
341 lNow = now;
342 }
343
344 public Calendar getCNow() {
345 return cNow;
346 }
347
348 public void setCNow(Calendar now) {
349 cNow = now;
350 }
351
352 public Date getDNow() {
353 return dNow;
354 }
355
356 public void setDNow(Date now) {
357 dNow = now;
358 }
359
360 public Timestamp getTNow() {
361 return tNow;
362 }
363
364 public void setTNow(Timestamp now) {
365 tNow = now;
366 }
367
368 public java.sql.Date getToday() {
369 return today;
370 }
371
372 public void setToday(java.sql.Date today) {
373 this.today = today;
374 }
375
376 public java.sql.Time getNow() {
377 return now;
378 }
379
380 public void setNow(java.sql.Time now) {
381 this.now = now;
382 }
383
384 /**
385 * @param args
386 */
387 public static void main(String[] args) {
388 // TODO Auto-generated method stub
389
390 }
391
392 }
393