Android时间转换工具Time

  1. 转自:http://blog.csdn.net/syif88/article/details/53517613

  2. import java.text.SimpleDateFormat;  
  3. import java.util.Calendar;  
  4. import java.util.Date;  
  5.   
  6.   
  7. /** 
  8.  * 时间转换工具 
  9.  */  
  10. public class TimeUtil {  
  11.   
  12.   
  13.     private TimeUtil(){}  
  14.   
  15.     /** 
  16.      * 时间转化为显示字符串 
  17.      * 
  18.      * @param timeStamp 单位为秒 
  19.      */  
  20.     public static String getTimeStr(long timeStamp){  
  21.         if (timeStamp==0return "";  
  22.         Calendar inputTime = Calendar.getInstance();  
  23.         inputTime.setTimeInMillis(timeStamp*1000);  
  24.         Date currenTimeZone = inputTime.getTime();  
  25.         Calendar calendar = Calendar.getInstance();  
  26.         calendar.set(Calendar.HOUR_OF_DAY, 23);  
  27.         calendar.set(Calendar.MINUTE, 59);  
  28.         if (calendar.before(inputTime)){  
  29.             //今天23:59在输入时间之前,解决一些时间误差,把当天时间显示到这里  
  30.             SimpleDateFormat sdf = new SimpleDateFormat("yyyy" +"年"+"MM"+"月"+"dd"+"日");  
  31.             return sdf.format(currenTimeZone);  
  32.         }  
  33.         calendar.set(Calendar.HOUR_OF_DAY, 0);  
  34.         calendar.set(Calendar.MINUTE, 0);  
  35.         calendar.set(Calendar.SECOND, 0);  
  36.         calendar.set(Calendar.MILLISECOND, 0);  
  37.         if (calendar.before(inputTime)){  
  38.             SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");  
  39.             return sdf.format(currenTimeZone);  
  40.         }  
  41.         calendar.add(Calendar.DAY_OF_MONTH,-1);  
  42.         if (calendar.before(inputTime)){  
  43.             return "昨天";  
  44.         }else{  
  45.             calendar.add(Calendar.DAY_OF_MONTH, -5);  
  46.             if (calendar.before(inputTime)){  
  47.                 return getWeekDayStr(inputTime.get(Calendar.DAY_OF_WEEK));  
  48.             }  
  49.             else  
  50.             {  
  51.                 calendar.set(Calendar.DAY_OF_MONTH, 1);  
  52.                 calendar.set(Calendar.MONTH, Calendar.JANUARY);  
  53.                 int year = inputTime.get(Calendar.YEAR);  
  54.                 int month = inputTime.get(Calendar.MONTH);  
  55.                 int day = inputTime.get(Calendar.DAY_OF_MONTH);  
  56.                 return year+"/"+month+"/"+day;  
  57.             }  
  58.   
  59.   
  60.         }  
  61.   
  62.     }  
  63.   
  64.     /** 
  65.      * 时间转化为聊天界面显示字符串 
  66.      * 
  67.      * @param timeStamp 单位为秒 
  68.      */  
  69.     public static String getChatTimeStr(long timeStamp){  
  70.         if (timeStamp==0return "";  
  71.         Calendar inputTime = Calendar.getInstance();  
  72.         inputTime.setTimeInMillis(timeStamp*1000);  
  73.         Date currenTimeZone = inputTime.getTime();  
  74.         Calendar calendar = Calendar.getInstance();  
  75.         if (calendar.before(inputTime)){  
  76.             //当前时间在输入时间之前  
  77.             SimpleDateFormat sdf = new SimpleDateFormat("yyyy" + "年"+"MM"+"月"+"dd"+"日");  
  78.             return sdf.format(currenTimeZone);  
  79.         }  
  80.         calendar.set(Calendar.HOUR_OF_DAY, 0);  
  81.         calendar.set(Calendar.MINUTE, 0);  
  82.         calendar.set(Calendar.SECOND, 0);  
  83.         calendar.set(Calendar.MILLISECOND, 0);  
  84.         if (calendar.before(inputTime)){  
  85.             SimpleDateFormat sdf = new SimpleDateFormat("h:mm");  
  86.             return timeFormatStr(inputTime,sdf.format(currenTimeZone));  
  87.         }  
  88.         calendar.add(Calendar.DAY_OF_MONTH,-1);  
  89.         if (calendar.before(inputTime)){  
  90.             SimpleDateFormat sdf = new SimpleDateFormat("h:mm");  
  91.             return "昨天"+" "+timeFormatStr(inputTime, sdf.format(currenTimeZone));  
  92.         }else{  
  93.             calendar.set(Calendar.DAY_OF_MONTH, 1);  
  94.             calendar.set(Calendar.MONTH, Calendar.JANUARY);  
  95.             if (calendar.before(inputTime)){  
  96.                 SimpleDateFormat sdf = new SimpleDateFormat("M"+"月"+"d"+"日");  
  97.                 String temp1 = sdf.format(currenTimeZone);  
  98.                 SimpleDateFormat sdf1 = new SimpleDateFormat("h:mm");  
  99.                 String temp2 = timeFormatStr(inputTime, sdf1.format(currenTimeZone));  
  100.                 return temp1+temp2;  
  101.             }else{  
  102.                 SimpleDateFormat sdf = new SimpleDateFormat("yyyy"+"年"+"M"+"月"+"d"+"日");  
  103.                 String temp1 = sdf.format(currenTimeZone);  
  104.                 SimpleDateFormat sdf1 = new SimpleDateFormat("h:mm");  
  105.                 String temp2 = timeFormatStr(inputTime, sdf1.format(currenTimeZone));  
  106.                 return temp1+temp2;  
  107.             }  
  108.   
  109.         }  
  110.   
  111.     }  
  112.   
  113.     /** 
  114.      * 24小时制转化成12小时制 
  115.      * 
  116.      * @param strDay 
  117.      */  
  118.     public static String timeFormatStr(Calendar calendar,String strDay)  
  119.     {  
  120.         String tempStr = "";  
  121.         int hour = calendar.get(Calendar.HOUR_OF_DAY);  
  122.         if (hour > 11)  
  123.         {  
  124.             tempStr = "下午"+" " + strDay;  
  125.         }  
  126.         else  
  127.         {  
  128.             tempStr = "上午"+" " + strDay;  
  129.         }  
  130.         return tempStr;  
  131.     }  
  132.   
  133.     /** 
  134.      * 时间转化为星期 
  135.      * 
  136.      * @param indexOfWeek   星期的第几天 
  137.      */  
  138.     public static String getWeekDayStr(int indexOfWeek){  
  139.         String weekDayStr = "";  
  140.         switch (indexOfWeek)  
  141.         {  
  142.             case 1:  
  143.                 weekDayStr = "星期日";  
  144.                 break;  
  145.             case 2:  
  146.                 weekDayStr = "星期一";  
  147.                 break;  
  148.             case 3:  
  149.                 weekDayStr = "星期二";  
  150.                 break;  
  151.             case 4:  
  152.                 weekDayStr = "星期三";  
  153.                 break;  
  154.             case 5:  
  155.                 weekDayStr = "星期四";  
  156.                 break;  
  157.             case 6:  
  158.                 weekDayStr = "星期五";  
  159.                 break;  
  160.             case 7:  
  161.                 weekDayStr = "星期六";  
  162.                 break;  
  163.         }  
  164.         return weekDayStr;  
  165.     }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值