DateFormat helps to the format and parse dates for any locale. Your code can be completely independent of the locale conventions for months, days of the week, or even the calendar forma T:lunar vs. Solar.
To format a date is Locale, use one of the static factory methods:
The code is as follows |
Copy Code |
myString = Dateformat.getdateinstance (). Format (mydate);
|
If you are formatting multiple dates, it's more efficient to get the format and use it multiple Doesn ' t have to fetch the information about the local language and country conventions times.
The code is as follows |
Copy Code |
DateFormat df = dateformat.getdateinstance (); for (int i = 0; i < a.length; ++i) { Output.println (Df.format (mydate[i]) + ";"); }
|
To format a number for a different locale, specify it in the call to Getdateinstance:
The code is as follows |
Copy Code |
DateFormat df = dateformat.getdateinstance (Dateformat.long, locale.france);
|
DateFormat can also be used to parse strings:
The code is as follows |
Copy Code |
MyDate = Df.parse (myString);
|
Example
The code is as follows |
Copy Code |
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); } |