You use the DatePicker
control to select a date.
<?xml version="1.0" encoding="utf-8"?> <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/dateDefault" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <DatePicker android:id="@+id/datePicker" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
The following code sets the DatePicker with Date.
/*ww w.j a va 2 s. c o m*/ package com.java2s.app; import android.app.Activity; import android.os.Bundle; import android.widget.DatePicker; import android.widget.TextView; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView dateDefault = (TextView) findViewById(R.id.dateDefault); DatePicker dp = (DatePicker) this.findViewById(R.id.datePicker); // subtract 1 from December (12) to set it to December dp.init(2008, 11, 10, null); // The month, and just the month, is zero-based. dateDefault.setText("Date defaulted to " + (dp.getMonth() + 1) + "/" + dp.getDayOfMonth() + "/" + dp.getYear()); } }
For the month, the internal value is zero-based, which means that January is 0 and December is 11.
If you do not set values for these controls, the default values will be the current date and time as known to the device.