Commit 3cc48092 by 张晋雄

时间类

parent 415875e0
package cn.yunliyunwai.beyondclouds.util;
import android.util.Log;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class TimeUtils {
public String toMoment(Date date){
Calendar now = Calendar.getInstance();
now.setTime(new Date());
Calendar to = Calendar.getInstance();
to.setTime(date);
int year = now.get(Calendar.YEAR) - to.get(Calendar.YEAR);;
int month = now.get(Calendar.MONTH) - to.get(Calendar.MONTH);;
int day = now.get(Calendar.DAY_OF_MONTH) -to.get(Calendar.DAY_OF_MONTH);
int hour = now.get(Calendar.HOUR) - to.get(Calendar.HOUR);
int minute = now.get(Calendar.MINUTE) - to.get(Calendar.MINUTE);
int second = now.get(Calendar.SECOND) - to.get(Calendar.SECOND);
if(year > 0){
return year+"年前";
}else if(year < 0){
return Math.abs(year)+"年后";
} else if(month > 0){
return month+"个月前";
}else if(month < 0){
return Math.abs(month)+"个月后";
} else if(day > 0){
return day+"天前";
}else if(day < 0){
return Math.abs(day)+"天后";
}else if(hour > 0){
return hour+"小时前";
}else if(hour < 0){
return Math.abs(hour)+"小时后";
}else if(minute > 0){
return minute+"分钟前";
}else if(minute != 0){
return Math.abs(minute)+"分钟后";
}else if(second>0){
return second+"秒前";
}else{
return Math.abs(second)+"秒后";
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment