DataPicker and TimePicker display time and date (displayed on screen), datepicker Date Format
public class MainActivity extends Activity {
private DatePicker date_picker;
private TimePicker time_picker;
private TextView tv_date;
private TextView tv_time;
@Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
// Initialization
Date_picker = (DatePicker) findViewById (R. id. date );
Time_picker = (TimePicker) findViewById (R. id. time );
TV _date = (TextView) findViewById (R. id. TV _date );
TV _time = (TextView) findViewById (R. id. TV _time );
// Obtain the current year, month, day, And Hour Information
Calendar c = Calendar. getInstance ();
Int year = c. get (Calendar. YEAR );
Int month = c. get (Calendar. MONTH );
Int day = c. get (Calendar. DAY_OF_MONTH );
Int hour = c. get (Calendar. HOUR_OF_DAY );
Int minute = c. get (Calendar. MINUTE );
// When the Calendar is used for display, the month starts from 0 and 1 is added for display.
TV _date.setText ("current date:" + year + "-" + (month + 1) + "-" + day );
TV _time.setText ("current time:" + hour + ":" + minute );
// The date listener is required during initialization.
Date_picker.init (year, month, day, new OnDateChangedListener ();
// Listener date change
Public void onDateChanged (DatePicker view, int year, int monthOfYear,
Int dayOfMonth ){
// TODO Auto-generated method stub
// Toast. makeText (MainActivity. this, "current time:" + year + "-" + (monthOfYear + 1) + "-" + dayOfMonth, 0 ). show ();
// The month starts with 0. You need to add 1 to the display.
TV _date.setText ("current date:" + year + "-" + (monthOfYear + 1) + "-" + dayOfMonth );
}
});
// Set the time to the 24-hour format
Time_picker.setIs24HourView (true );
// Time listener
Time_picker.setOnTimeChangedListener (new OnTimeChangedListener (){
Public void onTimeChanged (TimePicker view, int hourOfDay, int minute ){
// TODO Auto-generated method stub
TV _time.setText ("current time:" + hourOfDay + ":" + minute );
}
});