将string类型的时间,根据是否为今天,是否为今年分别进行不同的显示。
public static String formatTime(String data) {
if (!TextUtils.equals(data, "") && data != null) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date currentTime = new Date();
Date compareTime = sdf.parse(data);
if (DateUtils.isToday(compareTime.getTime())) {
return new SimpleDateFormat("HH:mm").format(compareTime);
} else {
if (currentTime.getYear() == compareTime.getYear()) {
return new SimpleDateFormat("MM-dd").format(compareTime);
}
return new SimpleDateFormat("yyyy-MM-dd").format(compareTime);
}
} catch (ParseException e) {
e.printStackTrace();
}
}
return "";
}