码迷,mamicode.com
首页 > 其他好文 > 详细

根据时间值 显示对应的字符串

时间:2015-07-28 10:51:06      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:java   date   

/**
	 * 获取显示时间
	 * 
	 * @param time
	 * @return
	 */
	public String getDate(long time) {

		if (time <= 0) {
			return "";
		}

		// 日期处理
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		int year = calendar.get(Calendar.YEAR);
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		int miunte = calendar.get(Calendar.MINUTE);
		int millisecond = calendar.get(Calendar.SECOND);

		// 评论日期
		Calendar commentCale = Calendar.getInstance();
		commentCale.setTime(new Date(time));
		int commDay = commentCale.get(Calendar.DAY_OF_MONTH);
		int commYear = commentCale.get(Calendar.YEAR);
		int commHour = commentCale.get(Calendar.HOUR_OF_DAY);
		int commMiunte = commentCale.get(Calendar.MINUTE);
		int commMonth = commentCale.get(Calendar.MONTH + 1);
		int commMillisecond = commentCale.get(Calendar.SECOND);

		// 刚刚
		// X秒前
		// X分钟前
		// X小时前
		// 昨天
		// X月X日
		// 2014年

		if (year - commYear > 1) {// 去年 显示年
			return commYear + "年";
		}
		if (year - commYear == 1) {// X月X日
			return commMonth + "月" + commDay + "日";
		}
		if (day - commDay == 1) {// 昨天
			return "昨天";
		}
		if (day - commDay == 0) {// 今天
			if (hour == commHour) {// 同一小时
				if (miunte == commMiunte) {// 同一分钟
					if (millisecond == commMillisecond) {
						return "刚刚";
					}
					return millisecond - commMillisecond + "秒前";
				}
				return miunte - commMiunte + "分钟前";
			}

			return hour - commHour + "小时前";
		}

		// 一般不会到这步
		DateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
		return format.format(new Date(time));

	}

版权声明:本文为博主原创文章,未经博主允许不得转载。

根据时间值 显示对应的字符串

标签:java   date   

原文地址:http://blog.csdn.net/liangrui1988/article/details/47100259

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!