/**
* 时间字符串转换为与当前的时间差
* @author Sigua.Huang
* @return
* @throws ParseException
* @date 2018年4月9日
*/
public static String WithCurrentTime(String timeStr) throws ParseException {
System.out.println(timeStr);
String withCurrentTime = null;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = dateFormat.parse(timeStr);
long startTime = dateFormat.parse(dateFormat.format(date)).getTime();
System.out.println(dateFormat.format(new Date()));
long endTime = dateFormat.parse(dateFormat.format(new Date())).getTime();
System.out.println("startTime:"+startTime);
System.out.println("endTime:"+endTime);
System.out.println("endTime-startTime:"+(endTime-startTime));
long years = (endTime-startTime)/(1000l*60l*60l*24l*365l);
System.out.println("years----------"+years);
if( years>=1 ){
withCurrentTime = years + "年前 ";
}else{
long months = (endTime-startTime)/(1000l*60l*60l*24l*30l);
System.out.println("months----------"+months);
if( months>=1 ){
withCurrentTime = months + "个月前 ";
}else{
long days = (endTime-startTime)/(1000l*60l*60l*24l);
System.out.println("days----------:"+days);
if(days>=1){
withCurrentTime = days + "天前 ";
}else{
int hours = (int)((endTime-startTime)/(1000*60*60));
System.out.println("hours----------"+hours);
if(hours>=1){
withCurrentTime = hours + "个小时前 ";
}else{
int minutes = (int) ((endTime-startTime)/(1000*60));
System.out.println("minutes----------"+minutes);
if(minutes>=1){
withCurrentTime = minutes + "分钟前 ";
}else{
withCurrentTime = "一分钟内";
}
}
}
}
}
return withCurrentTime;
}
public static void main(String[] args) throws ParseException {
// String withCurrentTime = WithCurrentTime("2018-03-19 09:44:00");
// System.out.println(withCurrentTime);
String withCurrentTime1 = WithCurrentTime("2018-04-10 08:08:00");
System.out.println(withCurrentTime1);
}
计算时间差样例
猜你喜欢
转载自blog.csdn.net/weixin_39247773/article/details/79882041
今日推荐
周排行