微信、qq时间格式模板
产品近来蛋疼,时间格式从做完到现在改了四遍了 ,最新的要求如下:
* 2分钟内 无显示
* 2分钟-24小时 HH:mm
* 昨天 昨天 HH:mm
* 前天 前天 HH:mm
* 今年 MM:DD HH:mm
* 去年 去年 MM:DD HH:mm
* 前年 前年 MM:DD HH:mm
* 更远 yyyy:MM:DD HH:mm
这不是问题,很快写完代码。
/**
* 将一个时间戳转换成提示性时间字符串,如
* 2分钟内 无显示
* 2分钟-24小时 HH:mm
* 昨天 昨天 HH:mm
* 前天 前天 HH:mm
* 一年内 MM:DD HH:mm
* 去年 去年 MM:DD HH:mm
* 前年 前年 MM:DD HH:mm
* 更远 yyyy:MM:DD HH:mm
* 毫秒计算
* @param charttime
* @return
*/
public static String convertChatDetailTimeFormat(long charttime) { long curTime = System.currentTimeMillis() ;
long time = curTime - charttime; XCApplication.base_log.i(XCConfig.TAG_SYSTEM_OUT, time + "---时间差" + time/ 1000/ 60 + "分钟");
XCApplication.base_log.i(XCConfig.TAG_SYSTEM_OUT, curTime + "---当前时间" + format(new Date(curTime), FORMAT_LONG_CN_1));
XCApplication.base_log.i(XCConfig.TAG_SYSTEM_OUT, charttime + "---chartTime" + format(new Date(charttime), FORMAT_LONG_CN_1)); if (time < 120 * 1000 && time >= 0) {
return "刚刚";
} else if (time >= 120 *1000 && time < 3600 * 24 * 1000) { return format(new Date(charttime), FORMAT_HH_MM); } else if (time >= 3600 * 24 * 1 * 1000 && time < 3600 * 24 * 2 * 1000) { return "昨天" + format(new Date(charttime), FORMAT_HH_MM); } else if (time >= 3600 * 24 * 2 * 1000 && time < 3600 * 24 * 3 * 1000) { return "前天" + format(new Date(charttime), FORMAT_HH_MM);
} else if (time >= 3600 * 24 * 3 * 1000 && time < 3600 * 24 * 365 * 1 * 1000) { return format(new Date(charttime), FORMAT_MM_DD_HH_MM);
} else if (time >= 3600 * 24 * 365 * 1 * 1000 && time < 3600 * 24 * 365 * 2 * 1000) { return "去年" + format(new Date(charttime), FORMAT_MM_DD_HH_MM);
} else if (time >= 3600 * 24 * 365 * 2 * 1000 && time < 3600 * 24 * 365 * 3 * 1000) { return "前年" + format(new Date(charttime), FORMAT_MM_DD_HH_MM);
} else if (time >= 3600 * 24 * 365 * 3 * 1000) { return format(new Date(charttime), FORMAT_LONG_CN_1);
} else {
return "刚刚";
}
}
这里就有一个小问题,就是自然日时间跨越实际日时间,有可能出现昨天的时间不显示昨天,而显示为HH:mm,于是测试找上门来,要求改,将2分钟-24小时的条件改为2分钟-今日内。
那么这里的需求就改为
* 2分钟内 无显示
* 2分钟-今日 HH:mm
* 昨天 昨天 HH:mm
* 前天 前天 HH:mm
* 今年 MM:DD HH:mm
* 去年 去年 MM:DD HH:mm
* 前年 前年 MM:DD HH:mm
* 更远 yyyy:MM:DD HH:mm 这也不是多大的问题,问题是在跨年的情况该如何,2015-01-01 00:01.001 的前三分钟接受的消息,也就是2014-12-31 该显示为昨天还是去年。如果信息的接收时间比时间还要大,该如何显示。
经过一番撕逼,终于敲定,这里为了产品再次修改,要求产品立字据啊,作为终极版本存在。
/**
* 终极方法
* 将一个时间戳转换成提示性时间字符串,如
* 2分钟内 无显示
* 2分钟-今天 2分钟-今天 HH:mm
* 昨天 昨天 HH:mm
* 前天 前天 HH:mm
* 今年 MM:DD HH:mm
* 去年 去年 MM:DD HH:mm
* 前年 前年 MM:DD HH:mm
* 更远 yyyy:MM:DD HH:mm
* 毫秒计算
* @param time
* @return
*/
public static String convertWEChartTimeFormatFinalMethed(long time) {
long curTime = System.currentTimeMillis() ;
String showTimeFormat = ""; long temp = curTime - time;
if (temp < 120 * 1000 && temp >= 0) {
showTimeFormat = "";
return showTimeFormat;
}
Date mayTime = new Date(time); // Date today = UtilDate.parse("2015-01-01 02:02:02.001", UtilDate.FORMAT_FULL);
Date today = new Date();
//时间值
String mayTime_FORMAT_SHORT = format(mayTime, FORMAT_SHORT);
String mayTime_FORMAT_SHORT_YEAR = getYear(mayTime); if(mayTime.after(today)){
//除此以外
showTimeFormat = format(mayTime, FORMAT_LONG_CN_1); } else {
if(mayTime_FORMAT_SHORT != null && !mayTime_FORMAT_SHORT.trim().toString().equals("")){
//今天的时间yyyy-MM-dd
String today_str = format(today, FORMAT_SHORT);
String thisYear_str = getYear(today); //昨天的时间 yyyy-MM-dd
Calendar calLastDay = Calendar.getInstance();
calLastDay.setTime(today);
calLastDay.add(Calendar.DAY_OF_YEAR, -1);
System.out.println("昨天:" + format(calLastDay.getTime(), FORMAT_SHORT));
String lastDay = format(calLastDay.getTime(), FORMAT_SHORT); //前天的时间 yyyy-MM-dd
Calendar calPreviousDay = Calendar.getInstance();
calPreviousDay.setTime(today);
calPreviousDay.add(Calendar.DAY_OF_YEAR, -2);
System.out.println("前天:" + format(calPreviousDay.getTime(), FORMAT_SHORT));
String previousDay = format(calPreviousDay.getTime(), FORMAT_SHORT); //去年的时间 yyyy
Calendar calLastYear = Calendar.getInstance();
calLastYear.setTime(today);
calLastYear.add(Calendar.YEAR, -1);
String lastYear = getYear(calLastYear.getTime());
System.out.println("去年:" + format(calLastYear.getTime(), FORMAT_SHORT)); //前年的时间 yyyy
Calendar calPreviousYear = Calendar.getInstance();
calPreviousYear.setTime(today);
calPreviousYear.add(Calendar.YEAR, -2);
String previousYear = getYear(calPreviousYear.getTime());
System.out.println("前年:" + format(calPreviousYear.getTime(), FORMAT_SHORT)); //首先判断是否是今天
if(mayTime_FORMAT_SHORT.equals(today_str)){
//今天,则显示为 13:12
showTimeFormat = format(mayTime, FORMAT_HH_MM);
} else if(mayTime_FORMAT_SHORT.equals(lastDay)){
//昨天
showTimeFormat = "昨天 " + format(mayTime,FORMAT_HH_MM); } else if(mayTime_FORMAT_SHORT.equals(previousDay)){
//昨天
showTimeFormat = "前天 " + format(mayTime,FORMAT_HH_MM); } else if(mayTime_FORMAT_SHORT_YEAR.equals(thisYear_str)){
//今年
showTimeFormat = format(mayTime, FORMAT_MM_DD_HH_MM);
} else if(mayTime_FORMAT_SHORT_YEAR.equals(lastYear)){
//去年
showTimeFormat = "去年 " + format(mayTime, FORMAT_MM_DD_HH_MM);
} else if(mayTime_FORMAT_SHORT_YEAR.equals(previousYear)){
//前年
showTimeFormat = "前年 " + format(mayTime, FORMAT_MM_DD_HH_MM);
} else {
//除此以外
showTimeFormat = format(mayTime, FORMAT_LONG_CN_1);
} }
} return showTimeFormat;
}
微信、qq时间格式模板的更多相关文章
- SQL Server日期时间格式转换字符串详解 (详询请加qq:2085920154)
在SQL Server数据库中,SQL Server日期时间格式转换字符串可以改变SQL Server日期和时间的格式,是每个SQL数据库用户都应该掌握的.本文我们主要就介绍一下SQL Server日 ...
- 记录微信小程序里自带 时间格式 工具
微信小程序里面自己给了一个时间工具,是用来记录log日志,感觉可以记录下来,所以拿来自己用,以此记录: 直接传入 日期对象 进入 formatTime //得到下面格式的时间格式2017/07/22 ...
- 【Thinkphp5】解决模板输出时间戳自动转换为时间格式的问题
背景: 数据库存储时间为时间戳,格式为varchar,模板输出时未进行时间格式化却输出了时间格式 如下图 (数据库存储的时间戳) (页面输出的时间) (未进行格式化的时间代码) (格式化后的时间代码) ...
- 微信小程序中new Date()转换时间时间格式时IOS不兼容的问题
本周写小程序,遇到的一个bug,在chrome上显示得好好的时间,一到Safari/iPhone 就报错 “invalid date”,时间格式为“2019.06.06 13:12:49”,然后利用n ...
- 【微信小程序】处理时间格式,时间戳转化展示时间格式问题,调用外部js的默认方法function的问题
默认的 小程序中new Date()显示的时间是这样的: 格式化时间的显示怎么做: 小程序的根目录下util目录下默认有一个util.js文件 其中util.js文件内容是: //数据转化 funct ...
- 关于微信小程序iOS端时间格式兼容问题
经过测试发现,当时间格式为 2018-08-08 08:00 ,需要将时间转为其他格式时,Android端转换成功,iOS端报错或是转为NaN 这是因为iOS端对符号‘ - ’不支持,也就是说iOS端 ...
- h5 录音 自动生成proto Js语句 UglifyJS-- 对你的js做了什么 【原码笔记】-- protobuf.js 与 Long.js 【微信开发】-- 发送模板消息 能编程与会编程 vue2入坑随记(二) -- 自定义动态组件 微信上传图片
得益于前辈的分享,做了一个h5录音的demo.效果图如下: 点击开始录音会先弹出确认框: 首次确认允许后,再次录音不需要再确认,但如果用户点击禁止,则无法录音: 点击发送 将录音内容发送到对话框中.点 ...
- asp.net微信开发第九篇----模板消息的使用
微信平台的模板消息,使用起来非常好,效果如下: 和平时我们微信中关注信用卡官方微信,如果消费了,信用卡官方微信就返回一个模板消息给我们告知,余额还有多少,消费了多少. 使用的步骤,我只简单介绍了怎么使 ...
- h5 网页版的微博微信QQ登录
一:微博 1,先说微博吧,首先你的去http://open.weibo.com/wiki/先注册账号,通过验证审核.然后的创建网页应用.微博审核不通过的原因就是域名和网站地址,一定要按实际写的.一定要 ...
随机推荐
- [安卓] 8、VIEW和SURFACEVIEW游戏框架
这是个简单的游戏框架,上图显示我们实现了屏幕上对象的位置控制.这里要1个简单的layout资源和2个java类:在MainActivity中主要和以往一样,唯一不同的是去除电池图标和标题等操作,然后第 ...
- [JS13] ActivetX
<HTML> <head> <title>JavaScript Unleashed</title> <script type="text ...
- Jenkins Git 中文乱码问题解决
解决方法: x:\Jenkins\jenkins.xml 新增蓝色粗体标记参数(-Dfile.encoding=utf-8),然后重启Jenkins服务,完毕! <arguments>-X ...
- PowerDesigner 15.1 安装步骤详细图解及破解
准备工作: 下载 PowerDesigner 15.1 的安装文件和破解文件 PowerDesigner 15.1 下载地址:http://pan.baidu.com/share/link?share ...
- 在Mac OS上安装Vagrant和Docker的教程
转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/128.html?1455808640 当听到很多人在说Docker是多么多 ...
- paip.log4j兼容linux windows 路径设置
paip.log4j兼容linux windows 路径设置 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog ...
- Xcode 重新下载项目配置文件
配置文件保存在: ~/Library/MobileDevice/Provisioning Profiles 可以按修改日期排序移走没用的配置文件或者干脆将目录重命名,备份好旧的配置文件后,到Xcode ...
- linux进阶
常用命令 rpm -q centos-release 查看centos版本 whereis java 查看文件安装路径 which java 查看可执行文件路径 echo $PATH echo $JA ...
- Explain in detail the steps/processes that occur from the moment you type a URL in a browser and hit enter
In an extremely rough and simplified sketch, assuming the simplest possible HTTP request, no proxies ...
- 设置Tomcat编码
设置Tomcat编码 <Connector port="8080" maxThreads="150" mi ...