本文主要介绍一下 android的时间显示的一些 固定的格式
不多说,上代码
// SampleActivity.java
package com.yline.dateshow;
import java.util.Calendar;
import android.app.Activity;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.widget.TextView;
public class SampleActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_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);
java.util.Date noteTS = Calendar.getInstance().getTime();
String delegate = "MM//dd//yyyy hh:mm aa"; // 09/21/2014 02:17 pm
textView1.setText("Found Time :: "+DateFormat.format(delegate, noteTS));
delegate = "MMM dd,yyyy h:mm aa"; // sep 21,2014 02:17 pm
textView2.setText("Found Time :: "+DateFormat.format(delegate, noteTS));
delegate = "MMMM dd,yyyy h:mmaa"; // september 21,2014 02:17pm
textView3.setText("Found Time :: "+DateFormat.format(delegate, noteTS));
delegate = "E,MMMM dd,yyyy h:mm:ss aa"; // wed,septempber 21,2014 02:17:48 pm
textView4.setText("Found Time :: "+DateFormat.format(delegate, noteTS));
delegate = "EEEE,MMMM dd,yyyy h:mm:ss aa"; // wednesday,septempber 21,2014 02:17:48 pm
textView5.setText("Found Time :: "+DateFormat.format(delegate, noteTS));
}
}
// activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dip" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dip" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dip" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dip" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dip" />
</LinearLayout>
运行图:
本文代码下载:(可运行)