Android Development Time Date format internationalization implementation method

Source: Internet
Author: User
Tags dateformat locale

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);
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.