首页 > 技术文章 > 日期处理类

eachann 2014-12-06 15:22 原文

  1 public class AbDateUtil {
  2     
  3     /** 时间日期格式化到年月日时分秒. */
  4     public static final String dateFormatYMDHMS = "yyyy-MM-dd HH:mm:ss";
  5     
  6     /** 时间日期格式化到年月日. */
  7     public static final String dateFormatYMD = "yyyy-MM-dd";
  8     
  9     /** 时间日期格式化到年月. */
 10     public static final String dateFormatYM = "yyyy-MM";
 11     
 12     /** 时间日期格式化到年月日时分. */
 13     public static final String dateFormatYMDHM = "yyyy-MM-dd HH:mm";
 14     
 15     /** 时间日期格式化到月日. */
 16     public static final String dateFormatMD = "MM/dd";
 17     
 18     /** 时分秒. */
 19     public static final String dateFormatHMS = "HH:mm:ss";
 20     
 21     /** 时分. */
 22     public static final String dateFormatHM = "HH:mm";
 23     
 24     /** 上午. */
 25     public static final String AM = "AM";
 26 
 27     /** 下午. */
 28     public static final String PM = "PM";
 29 
 30 
 31     /**
 32      * 描述:String类型的日期时间转化为Date类型.
 33      *
 34      * @param strDate String形式的日期时间
 35      * @param format 格式化字符串,如:"yyyy-MM-dd HH:mm:ss"
 36      * @return Date Date类型日期时间
 37      */
 38     public static Date getDateByFormat(String strDate, String format) {
 39         SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
 40         Date date = null;
 41         try {
 42             date = mSimpleDateFormat.parse(strDate);
 43         } catch (ParseException e) {
 44             e.printStackTrace();
 45         }
 46         return date;
 47     }
 48     
 49     /**
 50      * 描述:获取偏移之后的Date.
 51      * @param date 日期时间
 52      * @param calendarField Calendar属性,对应offset的值, 如(Calendar.DATE,表示+offset天,Calendar.HOUR_OF_DAY,表示+offset小时)
 53      * @param offset 偏移(值大于0,表示+,值小于0,表示-)
 54      * @return Date 偏移之后的日期时间
 55      */
 56     public Date getDateByOffset(Date date,int calendarField,int offset) {
 57         Calendar c = new GregorianCalendar();
 58         try {
 59             c.setTime(date);
 60             c.add(calendarField, offset);
 61         } catch (Exception e) {
 62             e.printStackTrace();
 63         }
 64         return c.getTime();
 65     }
 66     
 67     /**
 68      * 描述:获取指定日期时间的字符串(可偏移).
 69      *
 70      * @param strDate String形式的日期时间
 71      * @param format 格式化字符串,如:"yyyy-MM-dd HH:mm:ss"
 72      * @param calendarField Calendar属性,对应offset的值, 如(Calendar.DATE,表示+offset天,Calendar.HOUR_OF_DAY,表示+offset小时)
 73      * @param offset 偏移(值大于0,表示+,值小于0,表示-)
 74      * @return String String类型的日期时间
 75      */
 76     public static String getStringByOffset(String strDate, String format,int calendarField,int offset) {
 77         String mDateTime = null;
 78         try {
 79             Calendar c = new GregorianCalendar();
 80             SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
 81             c.setTime(mSimpleDateFormat.parse(strDate));
 82             c.add(calendarField, offset);
 83             mDateTime = mSimpleDateFormat.format(c.getTime());
 84         } catch (ParseException e) {
 85             e.printStackTrace();
 86         }
 87         return mDateTime;
 88     }
 89     
 90     /**
 91      * 描述:Date类型转化为String类型(可偏移).
 92      *
 93      * @param date the date
 94      * @param format the format
 95      * @param calendarField the calendar field
 96      * @param offset the offset
 97      * @return String String类型日期时间
 98      */
 99     public static String getStringByOffset(Date date, String format,int calendarField,int offset) {
100         String strDate = null;
101         try {
102             Calendar c = new GregorianCalendar();
103             SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
104             c.setTime(date);
105             c.add(calendarField, offset);
106             strDate = mSimpleDateFormat.format(c.getTime());
107         } catch (Exception e) {
108             e.printStackTrace();
109         }
110         return strDate;
111     }
112     
113 
114     /**
115      * 描述:Date类型转化为String类型.
116      *
117      * @param date the date
118      * @param format the format
119      * @return String String类型日期时间
120      */
121     public static String getStringByFormat(Date date, String format) {
122         SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
123         String strDate = null;
124         try {
125             strDate = mSimpleDateFormat.format(date);
126         } catch (Exception e) {
127             e.printStackTrace();
128         }
129         return strDate;
130     }
131     
132     /**
133      * 描述:获取指定日期时间的字符串,用于导出想要的格式.
134      *
135      * @param strDate String形式的日期时间,必须为yyyy-MM-dd HH:mm:ss格式
136      * @param format 输出格式化字符串,如:"yyyy-MM-dd HH:mm:ss"
137      * @return String 转换后的String类型的日期时间
138      */
139     public static String getStringByFormat(String strDate, String format) {
140         String mDateTime = null;
141         try {
142             Calendar c = new GregorianCalendar();
143             SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(dateFormatYMDHMS);
144             c.setTime(mSimpleDateFormat.parse(strDate));
145             SimpleDateFormat mSimpleDateFormat2 = new SimpleDateFormat(format);
146             mDateTime = mSimpleDateFormat2.format(c.getTime());
147         } catch (Exception e) {
148             e.printStackTrace();
149         }
150         return mDateTime;
151     }
152     
153     /**
154      * 描述:获取milliseconds表示的日期时间的字符串.
155      *
156      * @param milliseconds the milliseconds
157      * @param format  格式化字符串,如:"yyyy-MM-dd HH:mm:ss"
158      * @return String 日期时间字符串
159      */
160     public static String getStringByFormat(long milliseconds,String format) {
161         String thisDateTime = null;
162         try {
163             SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
164             thisDateTime = mSimpleDateFormat.format(milliseconds);
165         } catch (Exception e) {
166             e.printStackTrace();
167         }
168         return thisDateTime;
169     }
170     
171     /**
172      * 描述:获取表示当前日期时间的字符串.
173      *
174      * @param format  格式化字符串,如:"yyyy-MM-dd HH:mm:ss"
175      * @return String String类型的当前日期时间
176      */
177     public static String getCurrentDate(String format) {
178         AbLogUtil.d(AbDateUtil.class, "getCurrentDate:"+format);
179         String curDateTime = null;
180         try {
181             SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
182             Calendar c = new GregorianCalendar();
183             curDateTime = mSimpleDateFormat.format(c.getTime());
184         } catch (Exception e) {
185             e.printStackTrace();
186         }
187         return curDateTime;
188 
189     }
190 
191     /**
192      * 描述:获取表示当前日期时间的字符串(可偏移).
193      *
194      * @param format 格式化字符串,如:"yyyy-MM-dd HH:mm:ss"
195      * @param calendarField Calendar属性,对应offset的值, 如(Calendar.DATE,表示+offset天,Calendar.HOUR_OF_DAY,表示+offset小时)
196      * @param offset 偏移(值大于0,表示+,值小于0,表示-)
197      * @return String String类型的日期时间
198      */
199     public static String getCurrentDateByOffset(String format,int calendarField,int offset) {
200         String mDateTime = null;
201         try {
202             SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
203             Calendar c = new GregorianCalendar();
204             c.add(calendarField, offset);
205             mDateTime = mSimpleDateFormat.format(c.getTime());
206         } catch (Exception e) {
207             e.printStackTrace();
208         }
209         return mDateTime;
210 
211     }
212 
213     /**
214      * 描述:计算两个日期所差的天数.
215      *
216      * @param milliseconds1 the milliseconds1
217      * @param milliseconds2 the milliseconds2
218      * @return int 所差的天数
219      */
220     public static int getOffectDay(long milliseconds1, long milliseconds2) {
221         Calendar calendar1 = Calendar.getInstance();
222         calendar1.setTimeInMillis(milliseconds1);
223         Calendar calendar2 = Calendar.getInstance();
224         calendar2.setTimeInMillis(milliseconds2);
225         //先判断是否同年
226         int y1 = calendar1.get(Calendar.YEAR);
227         int y2 = calendar2.get(Calendar.YEAR);
228         int d1 = calendar1.get(Calendar.DAY_OF_YEAR);
229         int d2 = calendar2.get(Calendar.DAY_OF_YEAR);
230         int maxDays = 0;
231         int day = 0;
232         if (y1 - y2 > 0) {
233             maxDays = calendar2.getActualMaximum(Calendar.DAY_OF_YEAR);
234             day = d1 - d2 + maxDays;
235         } else if (y1 - y2 < 0) {
236             maxDays = calendar1.getActualMaximum(Calendar.DAY_OF_YEAR);
237             day = d1 - d2 - maxDays;
238         } else {
239             day = d1 - d2;
240         }
241         return day;
242     }
243     
244     /**
245      * 描述:计算两个日期所差的小时数.
246      *
247      * @param date1 第一个时间的毫秒表示
248      * @param date2 第二个时间的毫秒表示
249      * @return int 所差的小时数
250      */
251     public static int getOffectHour(long date1, long date2) {
252         Calendar calendar1 = Calendar.getInstance();
253         calendar1.setTimeInMillis(date1);
254         Calendar calendar2 = Calendar.getInstance();
255         calendar2.setTimeInMillis(date2);
256         int h1 = calendar1.get(Calendar.HOUR_OF_DAY);
257         int h2 = calendar2.get(Calendar.HOUR_OF_DAY);
258         int h = 0;
259         int day = getOffectDay(date1, date2);
260         h = h1-h2+day*24;
261         return h;
262     }
263     
264     /**
265      * 描述:计算两个日期所差的分钟数.
266      *
267      * @param date1 第一个时间的毫秒表示
268      * @param date2 第二个时间的毫秒表示
269      * @return int 所差的分钟数
270      */
271     public static int getOffectMinutes(long date1, long date2) {
272         Calendar calendar1 = Calendar.getInstance();
273         calendar1.setTimeInMillis(date1);
274         Calendar calendar2 = Calendar.getInstance();
275         calendar2.setTimeInMillis(date2);
276         int m1 = calendar1.get(Calendar.MINUTE);
277         int m2 = calendar2.get(Calendar.MINUTE);
278         int h = getOffectHour(date1, date2);
279         int m = 0;
280         m = m1-m2+h*60;
281         return m;
282     }
283 
284     /**
285      * 描述:获取本周一.
286      *
287      * @param format the format
288      * @return String String类型日期时间
289      */
290     public static String getFirstDayOfWeek(String format) {
291         return getDayOfWeek(format,Calendar.MONDAY);
292     }
293 
294     /**
295      * 描述:获取本周日.
296      *
297      * @param format the format
298      * @return String String类型日期时间
299      */
300     public static String getLastDayOfWeek(String format) {
301         return getDayOfWeek(format,Calendar.SUNDAY);
302     }
303 
304     /**
305      * 描述:获取本周的某一天.
306      *
307      * @param format the format
308      * @param calendarField the calendar field
309      * @return String String类型日期时间
310      */
311     private static String getDayOfWeek(String format,int calendarField) {
312         String strDate = null;
313         try {
314             Calendar c = new GregorianCalendar();
315             SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
316             int week = c.get(Calendar.DAY_OF_WEEK);
317             if (week == calendarField){
318                 strDate = mSimpleDateFormat.format(c.getTime());
319             }else{
320                 int offectDay = calendarField - week;
321                 if (calendarField == Calendar.SUNDAY) {
322                     offectDay = 7-Math.abs(offectDay);
323                 } 
324                 c.add(Calendar.DATE, offectDay);
325                 strDate = mSimpleDateFormat.format(c.getTime());
326             }
327         } catch (Exception e) {
328             e.printStackTrace();
329         }
330         return strDate;
331     }
332     
333     /**
334      * 描述:获取本月第一天.
335      *
336      * @param format the format
337      * @return String String类型日期时间
338      */
339     public static String getFirstDayOfMonth(String format) {
340         String strDate = null;
341         try {
342             Calendar c = new GregorianCalendar();
343             SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
344             //当前月的第一天
345             c.set(GregorianCalendar.DAY_OF_MONTH, 1);
346             strDate = mSimpleDateFormat.format(c.getTime());
347         } catch (Exception e) {
348             e.printStackTrace();
349         }
350         return strDate;
351 
352     }
353 
354     /**
355      * 描述:获取本月最后一天.
356      *
357      * @param format the format
358      * @return String String类型日期时间
359      */
360     public static String getLastDayOfMonth(String format) {
361         String strDate = null;
362         try {
363             Calendar c = new GregorianCalendar();
364             SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
365             // 当前月的最后一天
366             c.set(Calendar.DATE, 1);
367             c.roll(Calendar.DATE, -1);
368             strDate = mSimpleDateFormat.format(c.getTime());
369         } catch (Exception e) {
370             e.printStackTrace();
371         }
372         return strDate;
373     }
374 
375     
376 
377     /**
378      * 描述:获取表示当前日期的0点时间毫秒数.
379      *
380      * @return the first time of day
381      */
382     public static long getFirstTimeOfDay() {
383             Date date = null;
384             try {
385                 String currentDate = getCurrentDate(dateFormatYMD);
386                 date = getDateByFormat(currentDate+" 00:00:00",dateFormatYMDHMS);
387                 return date.getTime();
388             } catch (Exception e) {
389             }
390             return -1;
391     }
392     
393     /**
394      * 描述:获取表示当前日期24点时间毫秒数.
395      *
396      * @return the last time of day
397      */
398     public static long getLastTimeOfDay() {
399             Date date = null;
400             try {
401                 String currentDate = getCurrentDate(dateFormatYMD);
402                 date = getDateByFormat(currentDate+" 24:00:00",dateFormatYMDHMS);
403                 return date.getTime();
404             } catch (Exception e) {
405             }
406             return -1;
407     }
408     
409     /**
410      * 描述:判断是否是闰年()
411      * <p>(year能被4整除 并且 不能被100整除) 或者 year能被400整除,则该年为闰年.
412      *
413      * @param year 年代(如2012)
414      * @return boolean 是否为闰年
415      */
416     public static boolean isLeapYear(int year) {
417         if ((year % 4 == 0 && year % 400 != 0) || year % 400 == 0) {
418             return true;
419         } else {
420             return false;
421         }
422     }
423     
424     /**
425      * 描述:根据时间返回格式化后的时间的描述.
426      * 小于1小时显示多少分钟前  大于1小时显示今天+实际日期,大于今天全部显示实际时间
427      *
428      * @param strDate the str date
429      * @param outFormat the out format
430      * @return the string
431      */
432     public static String formatDateStr2Desc(String strDate,String outFormat) {
433         
434         DateFormat df = new SimpleDateFormat(dateFormatYMDHMS);
435         Calendar c1 = Calendar.getInstance();
436         Calendar c2 = Calendar.getInstance();
437         try {
438             c2.setTime(df.parse(strDate));
439             c1.setTime(new Date());
440             int d = getOffectDay(c1.getTimeInMillis(), c2.getTimeInMillis());
441             if(d==0){
442                 int h = getOffectHour(c1.getTimeInMillis(), c2.getTimeInMillis());
443                 if(h>0){
444                     return "今天"+getStringByFormat(strDate,dateFormatHM);
445                     //return h + "小时前";
446                 }else if(h<0){
447                     //return Math.abs(h) + "小时后";
448                 }else if(h==0){
449                     int m = getOffectMinutes(c1.getTimeInMillis(), c2.getTimeInMillis());
450                     if(m>0){
451                         return m + "分钟前";
452                     }else if(m<0){
453                         //return Math.abs(m) + "分钟后";
454                     }else{
455                         return "刚刚";
456                     }
457                 }
458                 
459             }else if(d>0){
460                 if(d == 1){
461                     //return "昨天"+getStringByFormat(strDate,outFormat);
462                 }else if(d==2){
463                     //return "前天"+getStringByFormat(strDate,outFormat);
464                 }
465             }else if(d<0){
466                 if(d == -1){
467                     //return "明天"+getStringByFormat(strDate,outFormat);
468                 }else if(d== -2){
469                     //return "后天"+getStringByFormat(strDate,outFormat);
470                 }else{
471                     //return Math.abs(d) + "天后"+getStringByFormat(strDate,outFormat);
472                 }
473             }
474             
475             String out = getStringByFormat(strDate,outFormat);
476             if(!AbStrUtil.isEmpty(out)){
477                 return out;
478             }
479         } catch (Exception e) {
480         }
481         
482         return strDate;
483     }
484     
485     
486     /**
487      * 取指定日期为星期几.
488      *
489      * @param strDate 指定日期
490      * @param inFormat 指定日期格式
491      * @return String   星期几
492      */
493     public static String getWeekNumber(String strDate,String inFormat) {
494       String week = "星期日";
495       Calendar calendar = new GregorianCalendar();
496       DateFormat df = new SimpleDateFormat(inFormat);
497       try {
498            calendar.setTime(df.parse(strDate));
499       } catch (Exception e) {
500           return "错误";
501       }
502       int intTemp = calendar.get(Calendar.DAY_OF_WEEK) - 1;
503       switch (intTemp){
504         case 0:
505           week = "星期日";
506           break;
507         case 1:
508           week = "星期一";
509           break;
510         case 2:
511           week = "星期二";
512           break;
513         case 3:
514           week = "星期三";
515           break;
516         case 4:
517           week = "星期四";
518           break;
519         case 5:
520           week = "星期五";
521           break;
522         case 6:
523           week = "星期六";
524           break;
525       }
526       return week;
527     }
528     
529     /**
530      * 根据给定的日期判断是否为上下午.
531      *
532      * @param strDate the str date
533      * @param format the format
534      * @return the time quantum
535      */
536     public static String getTimeQuantum(String strDate, String format) {
537         Date mDate = getDateByFormat(strDate, format);
538         int hour  = mDate.getHours();
539         if(hour >=12)
540            return "PM";
541         else
542            return "AM";
543     }
544     
545     /**
546      * 根据给定的毫秒数算得时间的描述.
547      *
548      * @param milliseconds the milliseconds
549      * @return the time description
550      */
551     public static String getTimeDescription(long milliseconds) {
552         if(milliseconds > 1000){
553             //大于一分
554             if(milliseconds/1000/60>1){
555                 long minute = milliseconds/1000/60;
556                 long second = milliseconds/1000%60;
557                 return minute+"分"+second+"秒";
558             }else{
559                 //显示秒
560                 return milliseconds/1000+"秒";
561             }
562         }else{
563             return milliseconds+"毫秒";
564         }
565     }
566     
567     /**
568      * The main method.
569      *
570      * @param args the arguments
571      */
572     public static void main(String[] args) {
573         System.out.println(formatDateStr2Desc("2012-3-2 12:2:20","MM月dd日  HH:mm"));
574     }
575 
576 }
View Code

author 还如一梦中

推荐阅读