In the long language version, the date and time format is a very frustrating thing, fortunately Android provides dateformate, can be formatted according to the default format of the specified language area.
public static Charsequence Formattimeinlistforoverseauser (
Final context, Final long, final Boolean simple,
Locale Locale) {
Final GregorianCalendar now = new GregorianCalendar ();
Special time
if (Time < Millseconds_of_hour) {
Return "";
}
Today
Final GregorianCalendar today = new GregorianCalendar (
Now.get (Gregoriancalendar.year),
Now.get (Gregoriancalendar.month),
Now.get (Gregoriancalendar.day_of_month));
Final Long in24h = Time-today.gettimeinmillis ();
if (in24h > 0 && in24h <= millseconds_of_day) {
Java.text.DateFormat df = java.text.DateFormat.getTimeInstance (
Java.text.DateFormat.SHORT, Locale);
Return "" + Df.format (time);
}
Yesterday
Final Long in48h = Time-today.gettimeinmillis () + millseconds_of_day;
if (in48h > 0 && in48h <= millseconds_of_day) {
Return simple? Context.getstring (R.string.fmt_pre_yesterday)
: Context.getstring (R.string.fmt_pre_yesterday)
+ " "
+ Java.text.DateFormat.getTimeInstance (
Java.text.DateFormat.SHORT, Locale). Format (
Time);
}
Final GregorianCalendar target = new GregorianCalendar ();
Target.settimeinmillis (time);
Same week
if (Now.get (gregoriancalendar.year) = = Target
. Get (Gregoriancalendar.year)
&& now.get (gregoriancalendar.week_of_year) = = Target
. Get (Gregoriancalendar.week_of_year)) {
Java.text.SimpleDateFormat SDF = new Java.text.SimpleDateFormat ("E", locale);
Final String Dow = "" + Sdf.format (time);
Return simple? Dow:dow
+ Java.text.DateFormat.getTimeInstance (
Java.text.DateFormat.SHORT, Locale). Format (time);
}
Same year
if (Now.get (gregoriancalendar.year) = = Target
. Get (Gregoriancalendar.year)) {
Return simple? Java.text.DateFormat.getDateInstance (
Java.text.DateFormat.SHORT, Locale). Format (Time)
: Java.text.DateFormat.getDateTimeInstance (
Java.text.DateFormat.SHORT,
Java.text.DateFormat.SHORT, Locale). Format (time);
}
Return simple? Java.text.DateFormat.getDateInstance (
Java.text.DateFormat.SHORT, Locale). Format (Time)
: Java.text.DateFormat.getDateTimeInstance (
Java.text.DateFormat.SHORT, Java.text.DateFormat.SHORT,
locale). Format (time);
}
Note that this is java.text.DateFormat and there is another java.text.format.DateFormat, which cannot specify locale.
Detailed introduction See: http://developer.android.com/reference/java/text/DateFormat.html