温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java如何实现显示当前运行时间

发布时间:2020-07-03 10:30:39 来源:亿速云 阅读:204 作者:Leah 栏目:编程语言

java如何实现显示当前运行时间?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

显示时间,如果与当前时间差别小于一天,则自动用**秒(分,小时)前,如果大于一天则用format规定的格式显示

/**
	 * 
	 * @author wxy
	 * @param ctime
	 *            时间
	 * @param format
	 *            格式 格式描述:例如:yyyy-MM-dd yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String showTime(Date ctime, String format) {
		//System.out.println("当前时间是:"+new Timestamp(System.currentTimeMillis()));
		
 
		//System.out.println("发布时间是:"+df.format(ctime).toString());
		String r = "";
		if(ctime==null)return r;
		if(format==null)format="MM-dd HH:mm";
 
		long nowtimelong = System.currentTimeMillis();
 
		long ctimelong = ctime.getTime();
		long result = Math.abs(nowtimelong - ctimelong);
 
		if(result < 60000){// 一分钟内
			long seconds = result / 1000;
			if(seconds == 0){
				r = "刚刚";
			}else{
				r = seconds + "秒前";
			}
		}else if (result >= 60000 && result < 3600000){// 一小时内
			long seconds = result / 60000;
			r = seconds + "分钟前";
		}else if (result >= 3600000 && result < 86400000){// 一天内
			long seconds = result / 3600000;
			r = seconds + "小时前";
		}else if (result >= 86400000 && result < 1702967296){// 三十天内
			long seconds = result / 86400000;
			r = seconds + "天前";
		}else{// 日期格式
			format="MM-dd HH:mm";
			SimpleDateFormat df = new SimpleDateFormat(format);
			r = df.format(ctime).toString();
		}
		return r;
	}

这里可以更具自己具体的需求进行扩展~~

public static void main(String[] args) {
try{
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(showTime(df.parse("2015-02-27 11:31:00"),"yyyy-MM-dd HH:mm:ss"));
}catch (Exception e) {
// TODO: handle exception
}
}

当前运行:4分钟前

关于java如何实现显示当前运行时间问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI