Search code examples
javaandroiddateandroid-datepickerandroid-date

How to show the next day's date from the selected date in the datepicker dialog in Android?


I am currently showing the today's date and day & as well as tomorrow's date and day in individual layouts while at the page load. But my requirement is that

1) Whatever the date I'm choosing from the Datepicker, it should display on the 1st layout

2) The next day's date should display on the second layout.

My 1st requirement is working fine without any problem.,(i.e), Whatever the day I'm choosing, it's displaying on the 1st layout. No issue, but I'm having problem with my 2nd requirement.

Consider if I choose 25-May-2018 on the DatePicker, it's displaying on the 1st layout as expected, but instead of displaying next day's date as 26-May-2018, it's displaying as 26-Jun-2018. But I need it to work in such a way that if I choose 25-May-2018 in Datepicker, the selected date should get displayed on 1st layout & the immediate next day's date (26-May-2018) should displayed on next layout. This should be applicable to whatever the date I'm choosing in the Datepicker.

Where have I gone wrong ?

Here's my Activity.Java:

public class PrivacyPolicyActivity extends AppCompatActivity {

    LinearLayout layout1, layout2, layout3, todayLayout, tomorrowLayout;
    ImageView calendarImgView;
    TextView todayDate;
    TextView todayDay;
    TextView tommmorrowDate;
    TextView tommorrowDay;
    TextView todayLabel;
    TextView tommorrowlabel;
    DatePickerDialog datePickerDialog;
    Calendar calendar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_privacy_policy);

        calendar = Calendar.getInstance();
        Date today = calendar.getTime();
        calendar.add(Calendar.DAY_OF_YEAR, 1);
        Date tomorrow = calendar.getTime();

        final DateFormat dateFormat = new SimpleDateFormat("dd-MMM");

        DateFormat day = new SimpleDateFormat("EEEE");

        layout1 = (LinearLayout) findViewById(R.id.layout1);
        layout2 = (LinearLayout) findViewById(R.id.layout2);
        layout3 = (LinearLayout) findViewById(R.id.layout3);
        todayDate = (TextView) findViewById(R.id.todayDate);

        todayDate.setText(dateFormat.format(today));
        todayDay = (TextView) findViewById(R.id.todayDay);
        todayDay.setText(day.format(today));

        tommmorrowDate = (TextView) findViewById(R.id.tommmorrowDate);
        tommmorrowDate.setText(dateFormat.format(tomorrow));
        tommorrowDay = (TextView) findViewById(R.id.tommorrowDay);
        tommorrowDay.setText(day.format(tomorrow));

        todayLayout = (LinearLayout) findViewById(R.id.todayLayout);
        tomorrowLayout = (LinearLayout) findViewById(R.id.tomorrowLayout);

        todayLabel = (TextView) findViewById(R.id.todayLabel);
        tommorrowlabel = (TextView) findViewById(R.id.tommorrowlabel);
        calendarImgView = (ImageView) findViewById(R.id.calendarImgView);

        calendarImgView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Calendar calendar = Calendar.getInstance();
                int year = calendar.get(Calendar.YEAR);
                int month = calendar.get(Calendar.MONTH);
                int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);

                datePickerDialog = new DatePickerDialog(PrivacyPolicyActivity.this,
                        new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker datePicker, int year, int month, int day) {

//                                SimpleDateFormat dateandmonth = new SimpleDateFormat("dd-MMM");
//                                Date date = new Date();
//                                String reqddate = dateandmonth.format(date);
                                //todayDate.setText(day + "/" + (month + 1) + "/" + year);

                                int cyear = datePicker.getYear();
                                int cmonth = datePicker.getMonth();
                                int cday = datePicker.getDayOfMonth();

                                Calendar cld = Calendar.getInstance();
                                cld.set(cyear, cmonth, cday);
                                SimpleDateFormat format = new SimpleDateFormat("dd-MMM");
                                String strDate = format.format(cld.getTime());
                                todayDate.setText(strDate);

                                SimpleDateFormat dayfmt = new SimpleDateFormat("EEEE");
                                String dayselected = dayfmt.format(cld.getTime());
                                todayDay.setText(dayselected);

                                int nextYear = datePicker.getYear() + 1;
                                int nextMonth = datePicker.getMonth() + 1;
                                int nextDay = datePicker.getDayOfMonth() + 1;

                                Calendar nextcld = Calendar.getInstance();
                                nextcld.set(nextYear, nextMonth, nextDay);

                                SimpleDateFormat tom_format = new SimpleDateFormat("dd-MMM");
                                String tom_date = tom_format.format(nextcld.getTime());
                                tommmorrowDate.setText(tom_date);

                                SimpleDateFormat day_fmt = new SimpleDateFormat("EEEE");
                                String tom_day = day_fmt.format(cld.getTime());
                                tommorrowDay.setText(tom_day);
                            }
                        }, year, month, dayOfMonth);

                datePickerDialog.show();
            }

        });
    }
}

Solution

  • Why you increasing Month and year also with 1 ?

    int nextYear = datePicker.getYear() + 1;
    int nextMonth = datePicker.getMonth() + 1;
    int nextDay = datePicker.getDayOfMonth() + 1;
    

    just increase nextDay.

    cld.add(Calendar.DATE, 1);
    

    Solution

     calendarImgView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Calendar calendar = Calendar.getInstance();
                    int year = calendar.get(Calendar.YEAR);
                    int month = calendar.get(Calendar.MONTH);
                    int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
    
                    datePickerDialog = new DatePickerDialog(PrivacyPolicyActivity.this,
                            new DatePickerDialog.OnDateSetListener() {
                                @Override
                                public void onDateSet(DatePicker datePicker, int year, int month, int day) {
    
    
    //                                SimpleDateFormat dateandmonth = new SimpleDateFormat("dd-MMM");
    
    
                                    int cyear = datePicker.getYear();
                                    int cmonth =datePicker .getMonth();
                                    int cday = datePicker.getDayOfMonth();
    
                                    Calendar cld  = Calendar.getInstance();
                                    cld.set(cyear, cmonth, cday);
                                    SimpleDateFormat format = new SimpleDateFormat("dd-MMM");
                                    String strDate = format.format(cld.getTime());
                                    todayDate.setText(strDate);
    
                                    SimpleDateFormat dayfmt = new SimpleDateFormat("EEEE");
                                    String dayselected = dayfmt.format(cld.getTime());
                                    todayDay.setText(dayselected);
    
    
                                    cld.add(Calendar.DATE, 1);
    
                                    SimpleDateFormat day_fmt = new SimpleDateFormat("EEEE");
                                    String tom_day = day_fmt.format(cld.getTime());
                                    tommorrowDay.setText(tom_day);
    
    
                                }
                            },year,month,dayOfMonth);
    
                    datePickerDialog.show();
                }
    
    
    
    
    
            });