日期:2014-05-17 浏览次数:20696 次
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import org.apache.commons.lang.time.DateFormatUtils;
public class DateUtils {
private final static long minute = 60 * 1000;// 1 minute
private final static long hour = 60 * minute;// 1 hour
private final static long day = 24 * hour;// 1 day
private final static long week = 7 * day;// 1 week
private final static long month = 31 * day;// 1 month
private final static long year = 12 * month;// 1 year
/**
* return the description of time period
*
* @author Cross
* @param date
* @return
*/
public static String getTimeFormatText(Date date) {
if (date == null) {
return null;
}
long diff = new Date().getTime() - date.getTime();
long r = 0;
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy", Locale.US);
/*
if (diff > year) {
r = (diff / year);
if(r == 1){
return r + "year ago";
}
return r + " years ago";
}
if (diff > month) {
r = (diff / month);
if(r == 1){
return r + "month ago";
}
return r + " months ago";
}
*/
if(diff > year
------解决方案--------------------
diff > month){
return sdf.format(date);
}
if(diff > week){
r = (diff /&nb