public static String getDayTime(String daytime){ //daytime 格式为yyyy-MM-dd HH:mm:ss
int mimutes = 0;
int minute = 0;
int days = 0;
int hours = 0;
String timeStr = "";
String time = "";
Date nowdate = new Date();
Date createdate = null;
if (daytime != null && !"".equals(daytime) && daytime.length()==19) {
time = daytime.substring(10,16);
try {
String pattern = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat format = new SimpleDateFormat(pattern);
createdate = format.parse(daytime);
} catch (ParseException e) {
e.printStackTrace();
}
if (nowdate.getDay()==createdate.getDay()) {
mimutes = TimeUtility.getMinutesDiff(nowdate,createdate);
if (mimutes > 0) {
hours = mimutes/24;
minute = mimutes%24;
if (hours == 0 && minute > 0) {
timeStr = String.valueOf(minute) + "分钟前";
}
if (hours > 0) {
if (hours <= 24 ) {
timeStr = String.valueOf(hours) + "小时前";
}
}
}
}else{
days = nowdate.getDay() - createdate.getDay();
if (days == 1) {
timeStr = "昨天" + time;
}else if (days == 2) {
timeStr = "前天" + time;
}else{
timeStr = daytime.substring(2,16);
}
}
}
return timeStr;
}
public static int getMinutesDiff(Date date1,Date date2){
Calendar c1 = Calendar.getInstance();
c1.setTime(date1);
Calendar c2 = Calendar.getInstance();
c2.setTime(date2);
return (int) (c1.getTimeInMillis()-c2.getTimeInMillis()) / (60 * 1000);
}
int mimutes = 0;
int minute = 0;
int days = 0;
int hours = 0;
String timeStr = "";
String time = "";
Date nowdate = new Date();
Date createdate = null;
if (daytime != null && !"".equals(daytime) && daytime.length()==19) {
time = daytime.substring(10,16);
try {
String pattern = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat format = new SimpleDateFormat(pattern);
createdate = format.parse(daytime);
} catch (ParseException e) {
e.printStackTrace();
}
if (nowdate.getDay()==createdate.getDay()) {
mimutes = TimeUtility.getMinutesDiff(nowdate,createdate);
if (mimutes > 0) {
hours = mimutes/24;
minute = mimutes%24;
if (hours == 0 && minute > 0) {
timeStr = String.valueOf(minute) + "分钟前";
}
if (hours > 0) {
if (hours <= 24 ) {
timeStr = String.valueOf(hours) + "小时前";
}
}
}
}else{
days = nowdate.getDay() - createdate.getDay();
if (days == 1) {
timeStr = "昨天" + time;
}else if (days == 2) {
timeStr = "前天" + time;
}else{
timeStr = daytime.substring(2,16);
}
}
}
return timeStr;
}
public static int getMinutesDiff(Date date1,Date date2){
Calendar c1 = Calendar.getInstance();
c1.setTime(date1);
Calendar c2 = Calendar.getInstance();
c2.setTime(date2);
return (int) (c1.getTimeInMillis()-c2.getTimeInMillis()) / (60 * 1000);
}