Android_Date显示时间格式_141027

本文介绍Android应用中使用DateFormat类设置不同格式的时间显示方法。通过SampleActivity示例代码展示了多种常见的时间格式,包括短日期加时间、完整星期名称等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文主要介绍一下 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>
运行图:



本文代码下载:(可运行)

http://pan.baidu.com/s/1GoS9K




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值