Formatting the Time and Date for Display
Published? true
FormatLanguage: WikiFormat
Problem:
I want to display the date and time in different standard formats. What is the the easiest approach to achieve this?
Solution:
DateFormat class provides APIs for formatting time and date in custom format. Using these APIs requires minimal efforts to fulfil the requirement.
Discussion:
Please refer the below code which adds 5 different TextViews for showing the time and date in different formats.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textview1"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textview2"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textview3"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textview4"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textview5"
/>
</LinearLayout>
Below code obtains the current time and date using java.util.Date class and then displays it in different formats
(Please refer the comments for sample outputs):
package com.sym.dateformatdemo;
import java.util.Calendar;
import android.app.Activity;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.widget.TextView;
public class TestDateFormatterActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView textView1 = (TextView) findViewById(R.id.textview1);
TextView textView2 = (TextView) findViewById(R.id.textview2);
TextView textView3 = (TextView) findViewById(R.id.textview3);
TextView textView4 = (TextView) findViewById(R.id.textview4);
TextView textView5 = (TextView) findViewById(R.id.textview5);
String delegate = "MM/dd/yy hh:mm a"; // 09/21/2011 02:17 pm
java.util.Date noteTS = Calendar.getInstance().getTime();
textView1.setText("Found Time :: "+DateFormat.format(delegate,noteTS));
delegate = "MMM dd, yyyy h:mm aa"; // Sep 21,2011 02:17 pm
textView2.setText("Found Time :: "+DateFormat.format(delegate,noteTS));
delegate = "MMMM dd, yyyy h:mmaa"; //September 21,2011 02:17pm
textView3.setText("Found Time :: "+DateFormat.format(delegate,noteTS));
delegate = "E, MMMM dd, yyyy h:mm:ss aa";//Wed, September 21,2011 02:17:48 pm
textView4.setText("Found Time :: "+DateFormat.format(delegate,noteTS));
delegate = "EEEE, MMMM dd, yyyy h:mm aa"; //Wednesday, September 21,2011 02:17:48 pm
textView5.setText("Found Time :: "+DateFormat.format(delegate,noteTS));
}
}
See Also:
Class
|
Usage
|
DateUtils
|
This class contains various date-related utilities for creating text for things like elapsed time and date ranges, strings for days of the week and months, and AM/ text etc.
|
Formatter
|
Utility class to aid in formatting common values that are not covered by java.util.Formatter
|
Time
|
The Time class is a faster replacement for the java.util.Calendar and java.util.GregorianCalendar classes.
|