Skip to content

Instantly share code, notes, and snippets.

@adamsp
Created December 16, 2013 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamsp/7993549 to your computer and use it in GitHub Desktop.
Save adamsp/7993549 to your computer and use it in GitHub Desktop.
Date formatting inconsistencies on Android. Varies across locales, Date format settings, devices, and presumably across Android versions too.
<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:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Current Locale:" />
<TextView
android:id="@+id/text_current_locale"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DateFormat.getDateFormat:" />
<TextView
android:id="@+id/text_short_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DateFormat.getMediumDateFormat:" />
<TextView
android:id="@+id/text_medium_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
package com.example.dtmformattingexamples;
import java.util.Date;
import java.util.Locale;
import android.app.Activity;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onResume() {
super.onResume();
((TextView) findViewById(R.id.text_current_locale)).setText(Locale
.getDefault().toString());
Date date = new Date();
// Returns a DateFormat object that can format the date in short form
// (such as 12/31/1999) according to the current locale and the user's
// date-order preference.
((TextView) findViewById(R.id.text_short_date)).setText(DateFormat
.getDateFormat(this).format(date));
// Returns a DateFormat object that can format the date in medium form
// (such as Jan 3, 2000) for the current locale.
((TextView) findViewById(R.id.text_medium_date)).setText(DateFormat
.getMediumDateFormat(this).format(date));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment