Android贴吧系统学习-----对时间的处理方法

1.需要做类似贴吧系统,必须要知道当前的帖子是几秒前发出的,还是几分前,几小时前,以及标准时间

对时间需要专门处理一个函数:

public class TimeHandler {
	public TimeHandler() {
	}

	@SuppressLint("SimpleDateFormat")
	public String handle(String time) {
		// 解析时间
		String result = "";
		Long current = System.currentTimeMillis();
		Long millionTime = Long.parseLong(time);
		Long delta = current - millionTime;
		if ((delta / 1000) / 60 < 1) {
			if ((delta / 1000) / 60 == 0)
				result = "2秒前";
			else
				result = (delta / 1000) + "秒前";
		} else {
			if ((delta / 1000) / 60 < 60)
				result = (delta / 1000) / 60 + "分前";
			else {
				if ((delta / 1000) / 3600 < 24)
					result = (delta / 1000) / 3600 + "小时前";
				else {
					SimpleDateFormat formatter = new SimpleDateFormat(
							"yyyy-MM-dd");
					Date date = new Date(millionTime);
					result = formatter.format(date);
				}
			}
		}
		return result;
	}
}


System.currentTimeMillis()产生一个当前的毫秒,这个毫秒其实就是自1970年1月1日0时起的毫秒数,

Date()其实就是相当于Date(System.currentTimeMillis());

因为Date类还有构造Date(long date),用来计算long秒与1970年1月1日之间的毫秒差。


得到了这个毫秒数,我们自己也可以算起现在的年月日周时,但是这不是我们去计算的,因为有Calendar。

Calendar最终出的结果就是年月日周时时区。


System.currentTimeMillis() 获得的是自1970-1-01 00:00:00.000 到当前时刻的时间距离,类型为long


String.valueOf(System.currentTimeMillis()) 这个语句可转为以下的型式:
long ct = System.currentTimeMillis();


String t = String.valueOf(ct);
其实上面的String t就相当于 ct+"";只是转为字符串格式


public String refFormatNowDate() {
  Date nowTime = new Date(System.currentTimeMillis());
  SimpleDateFormat sdFormatter = new SimpleDateFormat("yyyy-MM-dd");
  String retStrFormatNowDate = sdFormatter.format(nowTime);

  return retStrFormatNowDate;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值