格式化時間的函數

/**

* @param timeMills
*            格式化時間
* @param currentMills
*            當前時間
* @return 時間日期顯示,返回的格式有yy-MM-dd,星期幾 如果要顯示幾天前,幾分鐘前,幾秒前請使用DateUtils的相關方法
*/
public static String formatDateDisplayStyle(long date, long currentMills, Context context) {
Resources r = Resources.getSystem();
Locale locale = Locale.getDefault();
SimpleDateFormat sdf = null;
String returnStr = null;
int resId;
boolean past = (currentMills >= date);
long duration = currentMills - date;
long absDuration = Math.abs(duration);
returnStr += "past=" + past + "||absDuration=" + absDuration;
//如果大於當前時間或者在一個星期之前
if (!past || absDuration > WEEK_IN_MILLIS) {
if (locale.equals(Locale.CHINA)) {
sdf = new SimpleDateFormat("yy-M-d");
} else {
sdf = new SimpleDateFormat("M/d/yy");
}
returnStr = sdf.format(new Date(date));
return returnStr;
} else {
//如果是在24小時之內
if (absDuration < DAY_IN_MILLIS) {
Log.i("liyong","===24小時之內");
if (DateFormat.is24HourFormat(context)) {
Log.i("liyong", "====24小時制");
sdf = new SimpleDateFormat("H:MM");
} else {
locale.setDefault(Locale.ENGLISH);
if (locale.equals(Locale.CHINA)) {
Log.i("liyong", "====12中國片區");
sdf = new SimpleDateFormat("a hh:mm");
} else {
Log.i("liyong", "====12非中國片區");
sdf = new SimpleDateFormat("h:MM a");
}
}
Log.i("liyong", "new Date(date)=" + new Date(date));
returnStr = sdf.format(new Date(date));
Log.i("liyong", "returnStr=" + returnStr);
//如果是在24 -- 48小時之內
} else if (absDuration > DAY_IN_MILLIS && absDuration <= TWODAYS_IN_MILLIS) {
//resId = com.android.internal.R.plurals.abbrev_num_days_ago;
//returnStr = r.getQuantityString(resId, (int) 1);
//如果是在48 - 一個星期
} else if (absDuration > TWODAYS_IN_MILLIS && absDuration <= WEEK_IN_MILLIS) {
sdf = new SimpleDateFormat("EEEE");
returnStr = sdf.format(new Date(date));
}
}
return returnStr;
}
發佈了41 篇原創文章 · 獲贊 2 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章