Android Date-time format internationalization implementation code _android

Source: Internet
Author: User
Tags dateformat locale

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.

Directly paste code:

Copy Code code as follows:

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

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.