import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; import com.android.providers.downloads.ui.R; import android.content.ContentResolver; import android.content.Context; import android.text.format.DateFormat; import android.util.Log; /** * @author qqzhong Email:[email protected] * @version create time:2013-3-28 上午9:38:55 * @Description: */ public class TimeUtils { static public final int SECONDS_PER_HOUR = 3600; public static String convertDuration(long duration) { Calendar calendar = Calendar.getInstance(); // *1000 to convert seconds to millis. calendar.setTimeInMillis(duration * 1000); long hours = duration / (long) SECONDS_PER_HOUR; hours %= 100; duration /= SECONDS_PER_HOUR; String format = "mm:ss"; SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault()); String durationStr = sdf.format(calendar.getTime()); if (hours != 0) { durationStr = hours + ":" + durationStr; } return durationStr; } public static String formatDate(Context context, long dateInMillis) { Calendar nowCalendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(dateInMillis); String format = null; if (nowCalendar.get(Calendar.YEAR) != calendar.get(Calendar.YEAR)) { if (DateFormat.is24HourFormat(context)) { format = "yyyy/MM/dd HH:mm"; } else { format = "yyyy/MM/dd hh:mm a"; } } else if (nowCalendar.get(Calendar.DAY_OF_YEAR) != calendar .get(Calendar.DAY_OF_YEAR)) { if (DateFormat.is24HourFormat(context)) { format = "MM/dd HH:mm"; } else { format = "MM/dd hh:mm a"; } } else { if (DateFormat.is24HourFormat(context)) { format = "HH:mm"; } else { format = "hh:mm a"; } } SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault()); String dateStr = sdf.format(calendar.getTime()); return dateStr; } public static String formatHourseDate(Context context, long dateInMillis) { Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(dateInMillis); String format = null; if (DateFormat.is24HourFormat(context)) { format = "HH:mm"; } else { // "a hh:mm"表示 下午/下午 xx时xx分 例:“下午 3:24” 如果显示为 “3:24 下午” 就将 a 放在 hh:mm 后即可 format = "a hh:mm"; } SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault()); String dateStr = sdf.format(calendar.getTime()); return dateStr; } public static String timeShow(Context context, long timeMillis) { Date targetDate = new Date(timeMillis); Date nowDate = new Date(System.currentTimeMillis()); ContentResolver cv = context.getContentResolver(); boolean needDate = true; boolean needYear = (targetDate.getYear() != nowDate.getYear()); if ((targetDate.getYear() != nowDate.getYear())) { needYear = true; } else if ((targetDate.getMonth() != nowDate.getMonth()) || targetDate.getDate() != nowDate.getDate()) { needYear = false; } else { needDate = false; } String dateStr = ""; if (needDate) { boolean isZH = "zh".equals(context.getResources().getConfiguration().locale.getLanguage()); String defaultValue = android.provider.Settings.System.getString(cv, android.provider.Settings.System.DATE_FORMAT); String dateFormat = getDateFormatStringForSetting(context, defaultValue, isZH, needYear); dateStr = "00-00-00"; try { SimpleDateFormat sdf = new SimpleDateFormat(dateFormat); dateStr = sdf.format(targetDate); } catch (Exception e) { e.printStackTrace(); } } /* java.text.DateFormat tFormat = android.text.format.DateFormat.getTimeFormat(context); String timeStr = tFormat.format(timeMillis);*/ String timeStr = formatHourseDate(context,timeMillis); return dateStr+" "+timeStr; } private static String getDateFormatStringForSetting(Context context, String value, boolean isZH, boolean needYear) { String template = context.getString(com.android.internal.R.string.numeric_date_template); if (!needYear) { int first = template.indexOf("%s"); int second = template.indexOf("%s", first+1); template = template.substring(0, first) + template.substring(second); } if (value != null) { int month = value.indexOf('M'); int day = value.indexOf('d'); int year = value.indexOf('y'); if (month >= 0 && day >= 0 && year >= 0) { if (year < month && year < day) { if (month < day) { if (isZH) { if (!needYear) { value = context.getString(R.string.dateformat_md); } else { value = context.getString(R.string.dateformat_ymd); } } else { if (!needYear) { value = String.format(template, "MM", "dd"); } else { value = String.format(template, "yyyy", "MM", "dd"); } } } else { if (isZH) { if (!needYear) { value = context.getString(R.string.dateformat_dm); } else { value = context.getString(R.string.dateformat_ydm); } } else { if (!needYear) { value = String.format(template, "dd", "MM"); } else { value = String.format(template, "yyyy", "dd", "MM"); } } } } else if (month < day) { if (day < year) { if (isZH) { if (!needYear) { value = context.getString(R.string.dateformat_md); } else { value = context.getString(R.string.dateformat_mdy); } } else { if (!needYear) { value = String.format(template, "MM", "dd"); } else { value = String.format(template, "MM", "dd", "yyyy"); } } } else { // unlikely if (isZH) { if (!needYear) { value = context.getString(R.string.dateformat_md); } else { value = context.getString(R.string.dateformat_myd); } } else { if (!needYear) { value = String.format(template, "MM", "dd"); } else { value = String.format(template, "MM", "yyyy", "dd"); } } } } else { // day < month if (month < year) { if (isZH) { if (!needYear) { value = context.getString(R.string.dateformat_dm); } else { value = context.getString(R.string.dateformat_dmy); } } else { if (!needYear) { value = String.format(template, "dd", "MM"); } else { value = String.format(template, "dd", "MM", "yyyy"); } } } else { // unlikely if (isZH) { if (!needYear) { value = context.getString(R.string.dateformat_dm); } else { value = context.getString(R.string.dateformat_dym); } } else { if (!needYear) { value = String.format(template, "dd", "MM"); } else { value = String.format(template, "dd", "yyyy", "MM"); } } } } return value; } } if (isZH) { if (!needYear) { value = context.getString(R.string.dateformat_md); } else { value = context.getString(R.string.dateformat_ymd); } } else { if (!needYear) { value = String.format(template, "MM", "dd"); } else { value = String.format(template, "yyyy", "MM", "dd"); } } return value; } }
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="des_locked_icon">Locked Icon</string> <string name="des_message_sent_failure">Message sent failure</string> <string name="des_divider">Divider Line</string> <string name="des_quick_message">Show or hide quick message panel</string> <string name="des_send_message">Click to send message</string> <string name="des_draft_box_icon">Has draft</string> <string name="des_edit_phrase">Click to edit phrase</string> <string name="des_del_phrase">Click to delete phrase</string> <string name="des_del_recipient">Click to delete recipient</string> <string name="dateformat_ymd">yyyy年M月d日</string> <string name="dateformat_ydm">yyyy年d日M月</string> <string name="dateformat_myd">M月yyyy年d日</string> <string name="dateformat_mdy">M月d日yyyy年</string> <string name="dateformat_dym">d日yyyy年M月</string> <string name="dateformat_dmy">d日M月yyyy年</string> <string name="dateformat_md">M月d日</string> <string name="dateformat_dm">d日M月</string> </resources>