android开发中常见日期管理工具类

来源:互联网 发布:java排列组合算法 编辑:程序博客网 时间:2024/06/08 12:00

在日常的开发中,我们常常需要根据各种需要,对日期进行不同形式的格式化。这里我将日常用到的日期格式化加以总结。写出个工具方法,欢迎大家总结:

     1、对时间的加减操作,主要需求为闹钟等题型功能,用到的API为:Calendar  

             

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /** 
  2.      * 获取增加多少月的时间 
  3.      *  
  4.      * @return addMonth - 增加多少月 
  5.      */  
  6.     public static Date getAddMonthDate(int addMonth) {  
  7.         Calendar calendar = Calendar.getInstance();  
  8.         calendar.add(Calendar.MONTH, addMonth);  
  9.         return calendar.getTime();  
  10.     }  
  11.   
  12.     /** 
  13.      * 获取增加多少天的时间 
  14.      *  
  15.      * @return addDay - 增加多少天 
  16.      */  
  17.     public static Date getAddDayDate(int addDay) {  
  18.         Calendar calendar = Calendar.getInstance();  
  19.         calendar.add(Calendar.DAY_OF_YEAR, addDay);  
  20.         return calendar.getTime();  
  21.     }  
  22.     /** 
  23.      * 获取增加多少小时的时间 
  24.      *  
  25.      * @return addDay - 增加多少消失 
  26.      */  
  27.     public static Date getAddHourDate(int addHour) {  
  28.         Calendar calendar = Calendar.getInstance();  
  29.         calendar.add(Calendar.HOUR, addHour);  
  30.         return calendar.getTime();  
  31.     }  

2、按照  yyyy/MM/dd hh:mm等格式显示日期,主要用到的API为:SimpleDateFormat,Time

             

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /** 
  2.      * 显示时间格式为 hh:mm 
  3.      *  
  4.      * @param context 
  5.      * @param when 
  6.      * @return String 
  7.      */  
  8.     @SuppressLint("SimpleDateFormat")  
  9.     public static String formatTimeShort(Context context, long when) {  
  10.         String formatStr = "HH:mm";  
  11.         SimpleDateFormat sdf = new SimpleDateFormat(formatStr);  
  12.         String temp = sdf.format(when);  
  13.         if (temp != null && temp.length() == 5 && temp.substring(01).equals("0")) {  
  14.             temp = temp.substring(1);  
  15.         }  
  16.         return temp;  
  17.     }  
3、格式时间,让其输出,今天,昨天,两天前以及日期等。

         

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /** 
  2.      * 显示时间格式为今天、昨天、yyyy/MM/dd hh:mm 
  3.      *  
  4.      * @param context 
  5.      * @param when 
  6.      * @return String 
  7.      */  
  8.     public static String formatTimeString(Context context, long when) {  
  9.         Time then = new Time();  
  10.         then.set(when);  
  11.         Time now = new Time();  
  12.         now.setToNow();  
  13.   
  14.         String formatStr;  
  15.         if (then.year != now.year) {  
  16.             formatStr = "yyyy/MM/dd";  
  17.         } else if (then.yearDay != now.yearDay) {  
  18.             // If it is from a different day than today, show only the date.  
  19.             formatStr = "MM/dd";  
  20.         } else {  
  21.             // Otherwise, if the message is from today, show the time.  
  22.             formatStr = "HH:MM";  
  23.         }  
  24.   
  25.         if (then.year == now.year && then.yearDay == now.yearDay) {  
  26.             return context.getString(R.string.date_today);  
  27.         } else if ((then.year == now.year) && ((now.yearDay - then.yearDay) == 1)) {  
  28.             return context.getString(R.string.date_yesterday);  
  29.         } else {  
  30.             SimpleDateFormat sdf = new SimpleDateFormat(formatStr);  
  31.             String temp = sdf.format(when);  
  32.             if (temp != null && temp.length() == 5 && temp.substring(01).equals("0")) {  
  33.                 temp = temp.substring(1);  
  34.             }  
  35.             return temp;  
  36.         }  
  37.     }  
4、按照上下午显示时间:主要用到的API,DateUtils

             DateUtils.formatDateTime(this, System.getTimeMillis(), DateUtils.FORMAT_SHOW_TIME);

5、判断两个日期是否是同一个日期:

            

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /** 
  2.      * 是否同一天 
  3.      *  
  4.      * @param date1 
  5.      * @param date2 
  6.      * @return 
  7.      */  
  8.     public static boolean isSameDate(long date1, long date2) {  
  9.         long days1 = date1 / (1000 * 60 * 60 * 24);  
  10.         long days2 = date2 / (1000 * 60 * 60 * 24);  
  11.         return days1 == days2;  
0 0
原创粉丝点击