Android Calender Display Date and time

Source: Internet
Author: User

Paste the code First:


Java:

public class Mainactivity extends Activity {private int year;    private int month;    private int day;    private int hour;    private int minute;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        DatePicker DatePicker = (DatePicker) Findviewbyid (R.id.datepicker);        Timepicker Timepicker = (timepicker) Findviewbyid (R.id.timepicker);        Gets the current year, month, day, hour, minute Calendar C = calendar.getinstance ();        Year = C.get (calendar.year);        month = C.get (calendar.month);        Day = C.get (calendar.day_of_month);        hour = C.get (Calendar.hour);        minute = C.get (Calendar.minute);                    Initializes the DatePicker component, initializes the specified monitor datepicker.init (year, month, day, new Datepicker.ondatechangedlistener () {                        @Override public void ondatechanged (DatePicker arg0, int, int, month, Int. day) { MainacTivity.this.year = year;                        MainActivity.this.month = month;                        MainActivity.this.day = day;                    Displays the current date, time Showdate (year, month, day, hour, minute);        }        }        ); Specifies listener Timepicker.setontimechangedlistener for Timerpicker (new Timepicker.ontimechangedlistener () {@Overri De public void ontimechanged (timepicker view, int hourofday, int minute) {MainActivity.this.hou                R = Hourofday;                MainActivity.this.minute = minute;            Displays the current date, time Showdate (year, month, day, hour, minute);    }        }); }//define the method to display the current date and time in EditText private void showdate (int year, int month, int day, int hour, int minute) {E        Dittext show = (EditText) Findviewbyid (r.id.show);    Show.settext ("You have selected the date:" + year + "years" + month + "Month" + Day + "days" + Hour + "when" + Minute + "points"); }}


Xml:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:orientation=" vertical "android:layout_width=" match_parent "android:layout_height=" Ma        Tch_parent "> <textview android:layout_width=" match_parent "android:layout_height=" Wrap_content "        android:text= "Select specific Time"/> <!--define a DatePicker component--<datepicker android:id= "@+id/datepicker" Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "Center_h Orizontal "android:startyear=" "android:endyear=" 2030 "android:calendarviewshown=" true "an Droid:spinnersshown= "true" ></DatePicker> <!--define a Timepicker component--<timepicker Android:id = "@+id/timepicker" android:layout_width= "wrap_content" android:layout_height= "100DP" Android:layout_g Ravity= "Center_horizontal" &GT;</timepicker> <!--display user input date, time Control--<edittext android:id= "@+id/show" Android:layout_ Width= "Match_parent" android:layout_height= "Wrap_content" android:editable= "false" Android:cursorvisi Ble= "false"/></linearlayout>


The code is very simple, should be able to understand, start with the book to hit the code, the middle of the calendar only shows the week of the line, after the wrong datepicker found that the height of the set is not correct, modified to warp_content.

As follows:


Android Calender Display Date and time

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.