现在的位置: 首页 > 综合 > 正文

时间工具类(DateUtil)

2012年08月13日 ⁄ 综合 ⁄ 共 8878字 ⁄ 字号 评论关闭

虽然Apache下有个时间工具类的包,但这里写得时间工具类主要是自己在工作中常用的一些求时间的方法,见代码:

  1 [java] view plaincopyprint?
2 /**
3 * UtilsTest
4 * 时间日期工具类,封装工作中常用的一些时间日期计算方法等
5 * 还可以提供更多的重载方法,用于时间的转化等
6 */
7 package com.labci.util.test;
8 import java.text.DateFormat;
9 import java.text.ParseException;
10 import java.text.SimpleDateFormat;
11 import java.util.Calendar;
12 import java.util.Date;
13 import java.util.GregorianCalendar;
14 /**
15 * @author Bill Tu(tujiyue/iwtxokhtd)
16 * Jun 6, 2011[9:25:21 PM]
17 *
18 */
19 public class DateUtil {
20 private DateUtil(){
21
22 }
23
24 private static final String hhmmFormat="HH:mm";
25 private static final String MMddFormat="MM-dd";
26 private static final String yyyyFormat="yyyy";
27 private static final String yyyyChineseFormat="yyyy年";
28 private static final String yyyyMMddFormat="yyyy-MM-dd";
29 private static final String fullFormat="yyyy-MM-dd HH:mm:ss";
30 private static final String MMddChineseFormat="MM月dd日";
31 private static final String yyyyMMddChineseFormat="yyyy年MM月dd日";
32 private static final String fullChineseFormat="yyyy年MM月dd日 HH时mm分ss秒";
33 private static final String [] WEEKS={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
34
35 /**
36 * 得到指定时间的时间日期格式
37 * @param date 指定的时间
38 * @param format 时间日期格式
39 * @return
40 */
41 public static String getFormatDateTime(Date date,String format){
42 DateFormat df=new SimpleDateFormat(format);
43 return df.format(date);
44 }
45
46 /**
47 * 判断是否是润年
48 * @param date 指定的时间
49 * @return true:是润年,false:不是润年
50 */
51 public static boolean isLeapYear(Date date) {
52 Calendar cal=Calendar.getInstance();
53 cal.setTime(date);
54 return isLeapYear(cal.get(Calendar.YEAR));
55 }
56
57 /**
58 * 判断是否是润年
59 * @param date 指定的年
60 * @return true:是润年,false:不是润年
61 */
62 public static boolean isLeapYear(int year) {
63 GregorianCalendar calendar = new GregorianCalendar();
64 return calendar.isLeapYear(year);
65 }
66
67 /**
68 * 判断指定的时间是否是今天
69 * @param date 指定的时间
70 * @return true:是今天,false:非今天
71 */
72 public static boolean isInToday(Date date){
73 boolean flag=false;
74 Date now=new Date();
75 String fullFormat=getFormatDateTime(now,DateUtil.yyyyMMddFormat);
76 String beginString=fullFormat+" 00:00:00";
77 String endString=fullFormat+" 23:59:59";
78 DateFormat df=new SimpleDateFormat(DateUtil.fullFormat);
79 try {
80 Date beginTime=df.parse(beginString);
81 Date endTime=df.parse(endString);
82 flag=date.before(endTime)&&date.after(beginTime);
83 } catch (ParseException e) {
84 e.printStackTrace();
85 }
86 return flag;
87 }
88
89 /**
90 * 判断两时间是否是同一天
91 * @param from 第一个时间点
92 * @param to 第二个时间点
93 * @return true:是同一天,false:非同一天
94 */
95 public static boolean isSameDay(Date from,Date to){
96 boolean isSameDay=false;
97 DateFormat df=new SimpleDateFormat(DateUtil.yyyyMMddFormat);
98 String firstDate=df.format(from);
99 String secondDate=df.format(to);
100 isSameDay=firstDate.equals(secondDate);
101 return isSameDay;
102 }
103
104 /**
105 * 求出指定的时间那天是星期几
106 * @param date 指定的时间
107 * @return 星期X
108 */
109 public static String getWeekString(Date date){
110 return DateUtil.WEEKS[getWeek(date)-1];
111 }
112
113 /**
114 * 求出指定时间那天是星期几
115 * @param date 指定的时间
116 * @return 1-7
117 */
118 public static int getWeek(Date date){
119 int week=0;
120 Calendar cal=Calendar.getInstance();
121 cal.setTime(date);
122 week=cal.get(Calendar.DAY_OF_WEEK);
123 return week;
124 }
125
126 /**
127 * 取得指定时间离现在是多少时间以前,如:3秒前,2小时前等
128 * 注意:此计算方法不是精确的
129 * @param date 已有的指定时间
130 * @return 时间段描述
131 */
132 public static String getAgoTimeString(Date date){
133 Date now=new Date();
134 Calendar cal=Calendar.getInstance();
135 cal.setTime(date);
136 Date agoTime=cal.getTime();
137 long mtime=now.getTime()-agoTime.getTime();
138 String str="";
139 long stime=mtime/1000;
140 long minute=60;
141 long hour=60*60;
142 long day=24*60*60;
143 long weeks=7*24*60*60;
144 long months=100*24*60*60;
145 if(stime<minute){
146 long time_value=stime;
147 if(time_value<=0){
148 time_value=1;
149 }
150 str=time_value+"秒前";
151 }else if(stime>=minute && stime<hour){
152 long time_value=stime/minute;
153 if(time_value<=0){
154 time_value=1;
155 }
156 str=time_value+"分前";
157 }else if(stime>=hour && stime<day){
158 long time_value=stime/hour;
159 if(time_value<=0){
160 time_value=1;
161 }
162 str=time_value+"小时前";
163 }else if(stime>=day&&stime<weeks){
164 long time_value=stime/day;
165 if(time_value<=0){
166 time_value=1;
167 }
168 str=time_value+"天前";
169 }else if(stime>=weeks&&stime<months){
170 DateFormat df=new SimpleDateFormat(DateUtil.MMddFormat);
171 str=df.format(date);
172 }else{
173 DateFormat df=new SimpleDateFormat(DateUtil.yyyyMMddFormat);
174 str=df.format(date);
175 }
176 return str;
177 }
178
179 /**
180 * 判断指定时间是否是周末
181 * @param date 指定的时间
182 * @return true:是周末,false:非周末
183 */
184 public static boolean isWeeks(Date date){
185 boolean isWeek=false;
186 isWeek=(getWeek(date)-1==0||getWeek(date)-1==6);
187 return isWeek;
188 }
189
190 /**
191 * 得到今天的最开始时间
192 * @return 今天的最开始时间
193 */
194 public static Date getTodayBeginTime(){
195 String beginString=DateUtil.yyyyMMddFormat+" 00:00:00";
196 DateFormat df=new SimpleDateFormat(DateUtil.fullFormat);
197 Date beginTime=new Date();
198 try {
199 beginTime=df.parse(beginString);
200 } catch (ParseException e) {
201 e.printStackTrace();
202 }
203 return beginTime;
204 }
205
206 /**
207 * 得到今天的最后结束时间
208 * @return 今天的最后时间
209 */
210 public static Date getTodayEndTime(){
211 String endString=DateUtil.yyyyMMddFormat+" 23:59:59";
212 DateFormat df=new SimpleDateFormat(DateUtil.fullFormat);
213 Date endTime=new Date();
214 try {
215 endTime=df.parse(endString);
216 } catch (ParseException e) {
217 e.printStackTrace();
218 }
219 return endTime;
220 }
221
222 /**
223 * 取得本周的开始时间
224 * @return 本周的开始时间
225 */
226 public static Date getThisWeekBeginTime(){
227 Date beginTime=null;
228 Calendar cal=Calendar.getInstance();
229 int week=getWeek(cal.getTime());
230 week=week-1;
231 int days=0;
232 if(week==0){
233 days=6;
234 }else{
235 days=week-1;
236 }
237 cal.add(Calendar.DAY_OF_MONTH, -days);
238 beginTime=cal.getTime();
239 return beginTime;
240 }
241
242 /**
243 * 取得本周的开始日期
244 * @param format 时间的格式
245 * @return 指定格式的本周最开始时间
246 */
247 public static String getThisWeekBeginTimeString(String format){
248 DateFormat df=new SimpleDateFormat(format);
249 return df.format(getThisWeekBeginTime());
250 }
251
252
253 /**
254 * 取得本周的结束时间
255 * @return 本周的结束时间
256 */
257 public static Date getThisWeekEndTime(){
258 Date endTime=null;
259 Calendar cal=Calendar.getInstance();
260 int week=getWeek(cal.getTime());
261 week=week-1;
262 int days=0;
263 if(week!=0){
264 days=7-week;
265 }
266 cal.add(Calendar.DAY_OF_MONTH, days);
267 endTime=cal.getTime();
268 return endTime;
269 }
270
271
272 /**
273 * 取得本周的结束日期
274 * @param format 时间的格式
275 * @return 指定格式的本周结束时间
276 */
277 public static String getThisWeekEndTimeString(String format){
278 DateFormat df=new SimpleDateFormat(format);
279 return df.format(getThisWeekEndTime());
280 }
281
282 /**
283 * 取得两时间相差的天数
284 * @param from 第一个时间
285 * @param to 第二个时间
286 * @return 相差的天数
287 */
288 public static long getBetweenDays(Date from, Date to){
289 long days=0;
290 long dayTime=24*60*60*1000;
291 long fromTime=from.getTime();
292 long toTime=to.getTime();
293 long times=Math.abs(fromTime-toTime);
294 days=times/dayTime;
295 return days;
296 }
297
298 /**
299 * 取得两时间相差的小时数
300 * @param from 第一个时间
301 * @param to 第二个时间
302 * @return 相差的小时数
303 */
304 public static long getBetweenHours(Date from,Date to){
305 long hours=0;
306 long hourTime=60*60*1000;
307 long fromTime=from.getTime();
308 long toTime=to.getTime();
309 long times=Math.abs(fromTime-toTime);
310 hours=times/hourTime;
311 return hours;
312 }
313
314 /**
315 * 取得在指定时间上加减days天后的时间
316 * @param date 指定的时间
317 * @param days 天数,正为加,负为减
318 * @return 在指定时间上加减days天后的时间
319 */
320 public static Date addDays(Date date,int days){
321 Date time=null;
322 Calendar cal=Calendar.getInstance();
323 cal.add(Calendar.DAY_OF_MONTH, days);
324 time=cal.getTime();
325 return time;
326 }
327
328 /**
329 * 取得在指定时间上加减months月后的时间
330 * @param date 指定时间
331 * @param months 月数,正为加,负为减
332 * @return 在指定时间上加减months月后的时间
333 */
334 public static Date addMonths(Date date,int months){
335 Date time=null;
336 Calendar cal=Calendar.getInstance();
337 cal.add(Calendar.MONTH, months);
338 time=cal.getTime();
339 return time;
340 }
341
342 /**
343 * 取得在指定时间上加减years年后的时间
344 * @param date 指定时间
345 * @param years 年数,正为加,负为减
346 * @return 在指定时间上加减years年后的时间
347 */
348 public static Date addYears(Date date,int years){
349 Date time=null;
350 Calendar cal=Calendar.getInstance();
351 cal.add(Calendar.YEAR, years);
352 time=cal.getTime();
353 return time;
354 }
355
356
357
358 /**
359 * @param args
360 */
361 public static void main(String[] args) {
362 System.out.println(getFormatDateTime(new Date(),DateUtil.fullChineseFormat));
363 System.out.println(isLeapYear(new Date()));
364 Calendar cal=Calendar.getInstance();
365 System.out.println(isInToday(cal.getTime()));
366 Calendar cal2=Calendar.getInstance();
367 cal2.set(2011, 06, 05);
368 System.out.println(isSameDay(cal.getTime(),cal2.getTime()));
369 System.out.println(getWeekString(new Date()));
370 DateFormat df=new SimpleDateFormat(DateUtil.fullFormat);
371 String fullString="2011-06-03 22:37:20";
372 try {
373 Date fulldate=df.parse(fullString);
374 System.out.println(getBetweenDays(fulldate,cal.getTime()));
375 System.out.println(getAgoTimeString(fulldate));
376 System.out.println(isWeeks(fulldate));
377 } catch (ParseException e) {
378 e.printStackTrace();
379 }
380
381 System.out.println(getThisWeekBeginTimeString(DateUtil.yyyyMMddChineseFormat));
382 System.out.println(getThisWeekEndTimeString(DateUtil.yyyyMMddChineseFormat));
383 System.out.println(addDays(new Date(),3));
384 System.out.println(addDays(new Date(),-3));
385 System.out.println(addMonths(new Date(),2));
386 System.out.println(addMonths(new Date(),-2));
387 System.out.println(addYears(new Date(),1));
388 System.out.println(addYears(new Date(),-1));
389
390 }
391 }

抱歉!评论已关闭.