类似新浪微博发布时间显示方式 比如 **分钟前 ** 小时前 昨天12:30 前天13:30等
2013-11-06 09:00
423 查看
public static String getDayTime(String daytime){ //daytime 格式为yyyy-MM-dd HH:mm:ss
int mimutes = 0;
int minute = 0;
int days = 0;
int hours = 0;
String timeStr = "";
String time = "";
Date nowdate = new Date();
Date createdate = null;
if (daytime != null && !"".equals(daytime) && daytime.length()==19) {
time = daytime.substring(10,16);
try {
String pattern = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat format = new SimpleDateFormat(pattern);
createdate = format.parse(daytime);
} catch (ParseException e) {
e.printStackTrace();
}
if (nowdate.getDay()==createdate.getDay()) {
mimutes = TimeUtility.getMinutesDiff(nowdate,createdate);
if (mimutes > 0) {
hours = mimutes/24;
minute = mimutes%24;
if (hours == 0 && minute > 0) {
timeStr = String.valueOf(minute) + "分钟前";
}
if (hours > 0) {
if (hours <= 24 ) {
timeStr = String.valueOf(hours) + "小时前";
}
}
}
}else{
days = nowdate.getDay() - createdate.getDay();
if (days == 1) {
timeStr = "昨天" + time;
}else if (days == 2) {
timeStr = "前天" + time;
}else{
timeStr = daytime.substring(2,16);
}
}
}
return timeStr;
}
public static int getMinutesDiff(Date date1,Date date2){
Calendar c1 = Calendar.getInstance();
c1.setTime(date1);
Calendar c2 = Calendar.getInstance();
c2.setTime(date2);
return (int) (c1.getTimeInMillis()-c2.getTimeInMillis()) / (60 * 1000);
}
int mimutes = 0;
int minute = 0;
int days = 0;
int hours = 0;
String timeStr = "";
String time = "";
Date nowdate = new Date();
Date createdate = null;
if (daytime != null && !"".equals(daytime) && daytime.length()==19) {
time = daytime.substring(10,16);
try {
String pattern = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat format = new SimpleDateFormat(pattern);
createdate = format.parse(daytime);
} catch (ParseException e) {
e.printStackTrace();
}
if (nowdate.getDay()==createdate.getDay()) {
mimutes = TimeUtility.getMinutesDiff(nowdate,createdate);
if (mimutes > 0) {
hours = mimutes/24;
minute = mimutes%24;
if (hours == 0 && minute > 0) {
timeStr = String.valueOf(minute) + "分钟前";
}
if (hours > 0) {
if (hours <= 24 ) {
timeStr = String.valueOf(hours) + "小时前";
}
}
}
}else{
days = nowdate.getDay() - createdate.getDay();
if (days == 1) {
timeStr = "昨天" + time;
}else if (days == 2) {
timeStr = "前天" + time;
}else{
timeStr = daytime.substring(2,16);
}
}
}
return timeStr;
}
public static int getMinutesDiff(Date date1,Date date2){
Calendar c1 = Calendar.getInstance();
c1.setTime(date1);
Calendar c2 = Calendar.getInstance();
c2.setTime(date2);
return (int) (c1.getTimeInMillis()-c2.getTimeInMillis()) / (60 * 1000);
}
相关文章推荐
- 类似新浪微博发布时间显示方式 比如 **分钟前 ** 小时前 昨天12:30 前天13:30等
- 类似新浪微博动态发布时间转换机制,显示刚刚、几分钟前、几小时前、昨天、前天····
- 类似微信朋友圈将时间变成多少分钟、多少小时之前
- js jq插件 显示中文时间戳 刚刚 N分钟前 N小时前 今天 上午 下午 日期格式化
- php 显示某一个时间相当于当前时间在多少秒前,多少分钟前,多少小时前
- magento 后台模块开发之设定时间 显示小时和分钟
- TimePicker时间选择器,显示小时,分钟的
- linux初学<习题五>显示系统时间现实格式为:小时/分钟/秒AM/PM!
- 织梦前台模板页面实现显示发布时间格式为XX秒前、XX分钟前
- PHP CodeBase: 将时间显示为“刚刚”“n分钟/小时前”等
- PHP CodeBase: 将时间显示为“刚刚”“n分钟/小时前”等
- Java中时间间隔的计算(以前的时间与现在的时间间隔,类似于新浪微博显示发表时间的方式)
- Js Jquery 时间控件显示小时 分钟 秒
- PHP CodeBase:将时间显示为"刚刚""n分钟/小时前"的方法详解
- PHP CodeBase: 将时间显示为“刚刚”“n分钟/小时前”等
- PHP CodeBase:将时间显示为"刚刚""n分钟/小时前"的方法详解
- PHP人性化时间显示,实现多少秒前,多少分钟前,多少小时前
- JAVA数据库编程——JDBC(二):插入时间字段,显示小时分钟秒
- 在论坛中出现的比较难的sql问题:45(用户在线登陆时间的小时、分钟计算问题)
- C#有关时间处理的一些方法,两个日期间相差的天、小时、分钟、月数等