标签:
在控件中显示日期,并按自己的格式显示出来。其字符串格式如图1:
图1
例子:
在TextView中显示当前时间,运行效果:
源码:
activity_main.xml
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 tools:context=".MainActivity" > 6 7 <TextView 8 android:id="@+id/tvTime" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 /> 12 13 </RelativeLayout>
MainActivity.java
1 package xiongxuesong.test; 2 3 import java.text.SimpleDateFormat; 4 import java.util.Date; 5 import java.util.Locale; 6 7 import android.os.Bundle; 8 import android.app.Activity; 9 import android.view.Menu; 10 import android.widget.TextView; 11 12 public class MainActivity extends Activity { 13 14 private TextView mTextView; 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.activity_main); 19 20 String format = "yyyy年MM月dd日 HH:mm EEEE"; 21 SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US); 22 String textString = sdf.format(new Date()); 23 24 mTextView = (TextView) findViewById(R.id.tvTime); 25 mTextView.setText(textString); 26 } 27 28 @Override 29 public boolean onCreateOptionsMenu(Menu menu) { 30 // Inflate the menu; this adds items to the action bar if it is present. 31 getMenuInflater().inflate(R.menu.main, menu); 32 return true; 33 } 34 35 }
标签:
原文地址:http://www.cnblogs.com/xiongxuesong/p/4535617.html