This example show how to display formated Date using String.format() with Locale.
package com.example.androidstringformat;
import java.util.Date;
import java.util.Locale;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.os.Bundle;
public class MainActivity extends ActionBarActivity {
TextView textOut;
TextView localeInfo;
Spinner spAvailableLocale;
Locale[] availableLocales;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textOut = (TextView)findViewById(R.id.printout);
localeInfo = (TextView)findViewById(R.id.localeinfo);
//get installed locales
availableLocales = Locale.getAvailableLocales();
spAvailableLocale = (Spinner)findViewById(R.id.spavlocale);
ArrayAdapter<Locale> adapter =
new ArrayAdapter<Locale>(this,
android.R.layout.simple_spinner_item,
availableLocales);
adapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spAvailableLocale.setAdapter(adapter);
spAvailableLocale.setOnItemSelectedListener(onItemSelectedListener);
}
OnItemSelectedListener onItemSelectedListener =
new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Locale locale = (Locale)parent.getItemAtPosition(position);
localeInfo.setText(String.format(
"DisplayCountry: %s\nDisplayLanguage: %s\nDisplayName: %s\n\n",
locale.getDisplayCountry(),
locale.getDisplayLanguage(),
locale.getDisplayName()));
Date now = new Date();
textOut.setText(
String.format(locale, "%tc\n", now) + //C library asctime(3)-like output.
String.format(locale, "%tD\n", now) + //(MM/DD/YY)
String.format(locale, "%tF\n", now) + //(YYYY-MM-DD)
String.format(locale, "%tr\n", now) + //Full 12-hour time
String.format(locale, "%tz\n", now) + //Time zone GMT offset
String.format(locale, "%tZ\n", now) //Localized time zone abbreviation
);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {}
};
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.androidstringformat.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />
<Spinner
android:id="@+id/spavlocale"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/localeinfo"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/printout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
No comments:
Post a Comment