-
-
Save kassadin/35c20734bc9407f4a9d2 to your computer and use it in GitHub Desktop.
Android,智能格式化中文时间
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DateUtils { | |
public static String formatDateTime(Date date) { | |
String text; | |
long dateTime = date.getTime(); | |
if (isSameDay(dateTime)) { | |
Calendar calendar = GregorianCalendar.getInstance(); | |
if (inOneMinute(dateTime, calendar.getTimeInMillis())) { | |
return "刚刚"; | |
} else if (inOneHour(dateTime, calendar.getTimeInMillis())) { | |
return String.format("%d分钟之前", Math.abs(dateTime - calendar.getTimeInMillis()) / 60000); | |
} else { | |
calendar.setTime(date); | |
int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY); | |
if (hourOfDay > 17) { | |
text = "晚上 hh:mm"; | |
} else if (hourOfDay >= 0 && hourOfDay <= 6) { | |
text = "凌晨 hh:mm"; | |
} else if (hourOfDay > 11 && hourOfDay <= 17) { | |
text = "下午 hh:mm"; | |
} else { | |
text = "上午 hh:mm"; | |
} | |
} | |
} else if (isYesterday(dateTime)) { | |
text = "昨天 HH:mm"; | |
} else if (isSameYear(dateTime)) { | |
text = "M月d日 HH:mm"; | |
} else { | |
text = "yyyy-M-d HH:mm"; | |
} | |
// 注意,如果使用android.text.format.DateFormat这个工具类,在API 17之前它只支持adEhkMmszy | |
return new SimpleDateFormat(text, Locale.CHINA).format(date); | |
} | |
private static boolean inOneMinute(long time1, long time2) { | |
return Math.abs(time1 - time2) < 60000; | |
} | |
private static boolean inOneHour(long time1, long time2) { | |
return Math.abs(time1 - time2) < 3600000; | |
} | |
private static boolean isSameDay(long time) { | |
long startTime = floorDay(Calendar.getInstance()).getTimeInMillis(); | |
long endTime = ceilDay(Calendar.getInstance()).getTimeInMillis(); | |
return time > startTime && time < endTime; | |
} | |
private static boolean isYesterday(long time) { | |
Calendar startCal; | |
startCal = floorDay(Calendar.getInstance()); | |
startCal.add(Calendar.DAY_OF_MONTH, -1); | |
long startTime = startCal.getTimeInMillis(); | |
Calendar endCal; | |
endCal = ceilDay(Calendar.getInstance()); | |
endCal.add(Calendar.DAY_OF_MONTH, -1); | |
long endTime = endCal.getTimeInMillis(); | |
return time > startTime && time < endTime; | |
} | |
private static boolean isSameYear(long time) { | |
Calendar startCal; | |
startCal = floorDay(Calendar.getInstance()); | |
startCal.set(Calendar.MONTH, Calendar.JANUARY); | |
startCal.set(Calendar.DAY_OF_MONTH, 1); | |
return time >= startCal.getTimeInMillis(); | |
} | |
private static Calendar floorDay(Calendar startCal) { | |
startCal.set(Calendar.HOUR_OF_DAY, 0); | |
startCal.set(Calendar.MINUTE, 0); | |
startCal.set(Calendar.SECOND, 0); | |
startCal.set(Calendar.MILLISECOND, 0); | |
return startCal; | |
} | |
private static Calendar ceilDay(Calendar endCal) { | |
endCal.set(Calendar.HOUR_OF_DAY, 23); | |
endCal.set(Calendar.MINUTE, 59); | |
endCal.set(Calendar.SECOND, 59); | |
endCal.set(Calendar.MILLISECOND, 999); | |
return endCal; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment