简体   繁体   中英

How to get the first and last date of month in android datepicker

I'm trying to get user to select two dates, start Date and end Date, regardless of what the user selects i want to make sure the start date starts at the first date of the month and the end date ends with the last date of the month ie 31 or 30.

void selectStartDateForQtr()//DATEPICKER CONDITION
    {
        editTextPvcStartDate.setError(null);
        selectedDateCondition = "";
        DatePickerDialog mydate;
        final Calendar regDate = Calendar.getInstance();
        //int details = regDate.getTime();
        int day = regDate.get(Calendar.DAY_OF_MONTH);
        int month = regDate.get(Calendar.MONTH);
        int year = regDate.get(Calendar.YEAR);

        // date picker dialog
        mydate = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) 

{

 `enter code here`editTextPvcStartDate.setText(year + "-" + String.format("%02d",(monthOfYear + 1)) + "-" + String.format("%02d",dayOfMonth));


            }
        }, year, month, day);

        mydate.show();


    }

In android we can get the last date of the month like this way. Please check this code:

Calendar cal = Calendar.getInstance(); 
int res = cal.getActualMaximum(Calendar.DATE);
Log.e(TAG, "Today's Date =", cal.getTime());
Log.e(TAG, "Last Date of the current month = ", res);

Output:

Today's Date = Fri Nov 29 05:17:02 UTC 2019
Last Date of the current month = 30

Edited:

For example

LocalDate ld = datePicker.getValue();
Calendar c =  Calendar.getInstance();
c.set(ld.getYear(), ld.getMonthValue() - 1, ld.getDayOfMonth());
Date date = c.getTime();

In Month will start 0 to 11 so when you get and ans. of Datepicker value then month decrements 1 to set in GregorianCalendar.If you have doubt Please ask.

I hope it'll help you...!

Within your onDateSet function, try setting the values into a Calendar object and then using the functions from that to find your max:

date selectedDate = Calendar.getInstance()
selectedDate.set(year, monthOfYear, dayOfMonth)
date startDate = selectedDate
startDate.set(Calendar.DAY_OF_MONTH, 1)
date endDate = selectedDate
int lastDayOfMonth = endDate.getActualMaximum(Calendar.DAY_OF_MONTH)
endDate.set(Calendar.DAY_OF_MONTH, lastDayOfMonth)

Your startDate and endDate variables will now hold the 1st of the month and the last day of the month regardless of month size.

Try this code to get Selected moth first and last date

   //Change with selected date
     int iYear = 2020;
            int iMonth = Calendar.FEBRUARY; 
            int iDay = 1;

            // Create a calendar object and set year and month
            Calendar mycal = new GregorianCalendar(iYear, iMonth, iDay);


            int monthLastDate = mycal.getActualMaximum(Calendar.DAY_OF_MONTH);
            int monthFirstDate = mycal.getActualMinimum(Calendar.DAY_OF_MONTH);

            Log.e("check_days","----"+monthLastDate+"----"+monthFirstDate);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM