DataPicker and TimePicker display time and date (displayed on screen), datepicker Date Format

Source: Internet
Author: User

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 );
}
});

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.