android格式化时间中文版,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 仿微信聊天时间格式化显示功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/397413.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java分享第十七天-01(封装操作xml类)

做自动化测试的人&#xff0c;都应该对XPATH很熟悉了&#xff0c;但是在用JAVA解析XML时&#xff0c;我们通常是一层层的遍历进去&#xff0c;这样的代码的局限性很大&#xff0c;也不方便&#xff0c;于是我们结合一下XPATH&#xff0c;来解决这个问题。所需要的JAR包&#xf…

Ubuntu12.04 内核树建立

先查看自己使用的内核版本 linlin-virtual-machine:~$ uname -r 3.2.0-23-generic 如果安装系统时&#xff0c;自动安装了源码。在 /usr/src 目录下有对应的使用的版本目录。 linlin-virtual-machine:~$ cd /usr/src linlin-virtual-machine:/usr/src$ ls linux-headers-3.2.0…

【mysql】Innodb三大特性之double write

1、doublewrite buffer&#xff08;mysql官方的介绍&#xff09; InnoDB uses a novel file flush technique called doublewrite. Before writing pages to the data files, InnoDB first writes them to a contiguous area called the doublewrite buffer. Only after the wr…

android crop 大图,com.android.camera.action.CROP 实现图片剪裁

APP 中选取图片之后&#xff0c;有时候需要进行剪裁&#xff0c;比如头像。以下是启动代码。在我的项目中&#xff0c;传的是 filePath&#xff0c;所以我转了一下&#xff0c;但实际上从相册选择图片后&#xff0c;用 data.getData() 就可获得 uri。Uri uri Uri.fromFile(new…

Who Gets the Most Candies? POJ - 2886 (线段树)

按顺时针给出n个小孩&#xff0c;n个小孩每个人都有一个纸&#xff0c;然后每个人都有一个val&#xff0c;这个val等于自己的因子数&#xff0c;如果这个val是正的&#xff0c;那就顺时针的第val个孩子出去&#xff0c;如果是负的话&#xff0c;就逆时针的第val个孩子出去&…

javax.validation.ValidationException: Unable to find a default provider

2019独角兽企业重金招聘Python工程师标准>>> [ERROR] [2016-11-16 13:58:21 602] [main] (FrameworkServlet.java:457) Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name org.springframewo…

第十章练习题----2

package com.Hanqi2;public class xitizhuhanshu {public static void main(String[] args) {// TODO Auto-generated method stubxiti tm new xiti("黑色","15寸");xitizhs tm3 new xitizhs("蓝色","15寸");tm.Call("654"…

关于微信“被返回页”在被返回时自动刷新

网上有很多这些文章&#xff0c;但我觉得没一篇真正解决这个问题&#xff0c;倒是能给人一个解决方案的思路&#xff0c;对&#xff0c;就是posState事件。 要解决这个问题也不难&#xff0c;使用history的replaceState属性替换当前网页链接&#xff08;其实作用是在不增加hist…

android视频播放器api,03.视频播放器Api说明

03.视频播放器Api说明目录介绍01.最简单的播放02.如何切换视频内核03.切换视频模式04.切换视频清晰度05.视频播放监听06.列表中播放处理07.悬浮窗口播放08.其他重要功能Api09.播放多个视频10.VideoPlayer相关Api11.Controller相关Api12.边播放边缓存api13.类似抖音视频预加载14…

使用Python重命名MP3标签

从Window复制MP3文件的到Ubuntu下&#xff0c;MP3标签很多是乱码。于是想自己写个Python程序处理一下。 从酷狗复制过来的音乐文件名都是“作者 - 标题”&#xff0c;所以可以通过解析文件名直接获取作者和标题信息。 需要下载eyeD3模块 $ sudo apt-get install python-eyed3 代…

Taurus.MVC 2.0 开源发布:WebAPI开发教程

背景&#xff1a; 有用户反映&#xff0c;Tausus.MVC 能写WebAPI么&#xff1f; 能&#xff01; 教程呢&#xff1f; 嗯&#xff0c;木有&#xff01; 好吧&#xff0c;刚好2.0出来&#xff0c;就带上WEBAPI教程了&#xff01; 开源地址&#xff1a; https://github.com/cyq116…

android 锁屏 home,android 锁屏界面禁用长按home 和menu(recent apps)

android 5.1 系统中public long interceptKeyBeforeDispatching(WindowState win, KeyEvent event, int policyFlags) {//检查当前是否锁屏&#xff0c; 可以添加getTopApp()判断当前activity 来屏蔽2398 final boolean keyguardOn keyguardOn();添加新的方法&#xff1a;//获…

Chrome浏览器调试踩坑

Chrome浏览器若在响应式状态下&#xff0c;页面缩放比例不是100%&#xff0c;元素会“窜位”&#xff0c;点击元素会点击到元素周围的元素 Chrome页面缩放比例不为100%时&#xff0c;table的单元格就算没有边框&#xff08;CSS去掉了&#xff09;也会显示出边框&#xff08;缝隙…

WordPress 博客文章时间格式the_time()设置

国外设计的WordPress 主题里的文章的时间格式是类似“十一月 21, 2010”这种格式的&#xff0c;而中国人习惯的是年在前&#xff0c;月紧跟其后&#xff0c;日在末尾&#xff0c;所以看国外的就显得很别扭&#xff0c;但是我们可以通过修改WP时间代码来设置成为我们中国人习惯的…

linux yum

更改linux YUM源方法&#xff1a;第一步&#xff1a;进入yum配置文件目录&#xff1a;cd /etc/yum.repos.d/第二步&#xff1a;备份配置文件&#xff1a;mv CentOS-Base.repo CentOS-Base.repo.bak第三步&#xff1a;下载网易的配置&#xff08;或其他源配置文件&#xff09;&a…

chrome瀏覽器去掉緩存的方法

方法一&#xff1a; 1.開發說打開開發者工具 勾選這個訪問可以 方法二: commandshiftR 转载于:https://www.cnblogs.com/kaibindirver/p/9378572.html

Apache Tomcat目录下各个文件夹的作用

1.bin&#xff1a;存放各种不同平台开启与关闭Tomcat的脚本文件。 2.lib&#xff1a;存tomcat与web应用的Jar包。 3.conf&#xff1a;存放tomcat的配置文件。 4.webapps&#xff1a;web应用的发布目录。 5.work&#xff1a;tomcat把由各种jsp生成的servlet文件存放的地方。 6.l…

sony z2 android 5.0,索尼Xperia Z2 5.0 root教程_索尼Z2获取5.0系统的root

来说一下咱们的索尼Xperia Z2手机的5.0系统的root&#xff0c;因为现在很多机友的系统是5.0的&#xff0c;可是对于5.0的系统很多机友还不知道如何进行root操作&#xff0c;之前的针对4.4的系统的root方法肯定是用不到5.0的系统上的&#xff0c;因此需要专门的针对5.0的root软件…

ABP文档 - Javascript Api - AJAX

本节内容&#xff1a; AJAX操作相关问题ABP的方式 AJAX 返回信息处理错误 HTTP 状态码WrapResult和DontWrapResult特性 Asp.net Mvc 控制器Asp.net Web Api 控制器动态Web Api层Asp.net Core 控制器动态Web Api层AJAX操作相关问题 执行一个AJAX调用在现在的应用里非常常见&…

视达配色教程17 灰色的色彩意象是什么

视达配色教程17 灰色的色彩意象是什么 一、总结 一句话总结&#xff1a;没有个性的色彩 1、灰色的一般意象是什么&#xff1f; 所有混沌的情感不友好的色彩可怕、恐怖和残忍感情贫乏或者内向年龄和年老遗忘的过去贫困与谦虚劣等的颜色秘密与非法合适的中等-男式时装的标准 二、…