发布于 2017-03-11 01:05:02 | 151 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要介绍了Android 仿微信聊天时间格式化显示功能,本文中还给大家简单介绍了在同一年的显示规则和不在同一年的显示规则。需要的朋友可以参考下

本文给大家分享android仿微信聊天时间格式化显示功能。

在同一年的显示规则:

如果是当天显示格式为 HH:mm 例:14:45

如果是昨天,显示格式为 昨天 HH:mm 例:昨天 13:12

如果是在同一周 显示格式为 周一 HH:mm 例:周一14:05

如果不是同一周则显示格式为 M月d日 早上或者其它 HH:mm 例: 2月5日 早上10:10

不在同一年的显示规则:

显示格式为 yyyy年M月d日 晚上或者其它 HH:mm 例:2016年2月5日 晚上18:05

代码中如果有误,请留言。

代码实现如下:


import Java.text.SimpleDateFormat; 
import java.util.Calendar; 
import java.util.Date;
public class Test {
public static void main(String[] args) {
  System.out.println("当前时间:"+new SimpleDateFormat("yyyy/M/d HH:mm:ss").format(System.currentTimeMillis()));
  System.out.println("2016/2/1 05:05:00 显示为:"+getNewChatTime(1454666700000l));
  System.out.println("2017/2/1 05:05:00 显示为:"+getNewChatTime(1485983100000l));
  System.out.println("2017/2/4 12:05:00 显示为:"+getNewChatTime(1486181100000l));
  System.out.println("2017/2/5 10:10:00 显示为:"+getNewChatTime(1486260600000l));
  System.out.println("2017/2/5 13:12:00 显示为:"+getNewChatTime(1486271520000l));
  System.out.println("2017/2/6 14:05:00 显示为:"+getNewChatTime(1486361100000l));
  /*运行结果:
  当前时间:2017/2/9 14:36:36
  2016/2/1 05:05:00 显示为:2016年2月5日 晚上18:05
  2017/2/1 05:05:00 显示为:2月2日 凌晨05:05
  2017/2/4 12:05:00 显示为:2月4日 中午12:05
  2017/2/5 13:12:00 显示为:2月5日 早上10:10
  2017/2/5 13:12:00 显示为:2月5日 下午13:12
  2017/2/6 14:05:00 显示为:周一14:05*/
}
/**
 * 时间戳格式转换
 */
static String dayNames[] = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
public static String getNewChatTime(long timesamp) {
  String result = "";
  Calendar todayCalendar = Calendar.getInstance(); 
  Calendar otherCalendar = Calendar.getInstance(); 
  otherCalendar.setTimeInMillis(timesamp);
  String timeFormat="M月d日 HH:mm";
  String yearTimeFormat="yyyy年M月d日 HH:mm";
  String am_pm="";
  int hour=otherCalendar.get(Calendar.HOUR_OF_DAY);
  if(hour>=0&&hour<6){
    am_pm="凌晨";
  }else if(hour>=6&&hour<12){
    am_pm="早上";
  }else if(hour==12){
    am_pm="中午";
  }else if(hour>12&&hour<18){
    am_pm="下午";
  }else if(hour>=18){
    am_pm="晚上";
  }
  timeFormat="M月d日 "+ am_pm +"HH:mm";
  yearTimeFormat="yyyy年M月d日 "+ am_pm +"HH:mm";
  boolean yearTemp = todayCalendar.get(Calendar.YEAR)==otherCalendar.get(Calendar.YEAR);
  if(yearTemp){
    int todayMonth=todayCalendar.get(Calendar.MONTH);
    int otherMonth=otherCalendar.get(Calendar.MONTH);
    if(todayMonth==otherMonth){//表示是同一个月
      int temp=todayCalendar.get(Calendar.DATE)-otherCalendar.get(Calendar.DATE);
      switch (temp) {
      case 0:
        result = getHourAndMin(timesamp);
        break;
      case 1:
        result = "昨天 " + getHourAndMin(timesamp);
        break;
      case 2:
      case 3:
      case 4:
      case 5:
      case 6:
        int dayOfMonth = otherCalendar.get(Calendar.WEEK_OF_MONTH);
        int todayOfMonth=todayCalendar.get(Calendar.WEEK_OF_MONTH);
        if(dayOfMonth==todayOfMonth){//表示是同一周
          int dayOfWeek=otherCalendar.get(Calendar.DAY_OF_WEEK);
          if(dayOfWeek!=1){//判断当前是不是星期日   如想显示为:周日 12:09 可去掉此判断
            result = dayNames[otherCalendar.get(Calendar.DAY_OF_WEEK)-1] + getHourAndMin(timesamp);
          }else{
            result = getTime(timesamp,timeFormat);
          }
        }else{ 
          result = getTime(timesamp,timeFormat);
        }
        break;
      default:
        result = getTime(timesamp,timeFormat);
        break;
      }
    }else{
      result = getTime(timesamp,timeFormat);
    }
  }else{
    result=getYearTime(timesamp,yearTimeFormat);
  }
  return result;
}
/**
 * 当天的显示时间格式
 * @param time
 * @return
 */
public static String getHourAndMin(long time) {
  SimpleDateFormat format = new SimpleDateFormat("HH:mm");
  return format.format(new Date(time));
}
/**
 * 不同一周的显示时间格式
 * @param time
 * @param timeFormat
 * @return
 */
public static String getTime(long time,String timeFormat) {
  SimpleDateFormat format = new SimpleDateFormat(timeFormat);
  return format.format(new Date(time));
}
/**
 * 不同年的显示时间格式
 * @param time
 * @param yearTimeFormat
 * @return
 */
public static String getYearTime(long time,String yearTimeFormat) {
  SimpleDateFormat format = new SimpleDateFormat(yearTimeFormat);
  return format.format(new Date(time));
}
}

以上所述是小编给大家介绍的Android 仿微信聊天时间格式化显示功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对PHPERZ网站的支持!



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务