0

i create a booking hotels apps and it hase 3 textview and 2 button to choose the date. if button click it display a date and selected date will displayed to 2 textview. how to display the day between dates in the third textview?? here's my code ( the third textview not displayed as day between dates)

protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);


            dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.US);

            tvDateResult = (TextView) findViewById(R.id.tv_dateresult);
            res = findViewById(R.id.tv_dateresult1);
            ress = findViewById(R.id.tv_dateresult2);
            btDatePicker = (Button) findViewById(R.id.bt_datepicker);
            date = findViewById(R.id.bt_datepicker1);
            get = findViewById(R.id.getprice);

            date.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    showDateDialog2();
                }

            });

            btDatePicker.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    showDateDialog();
                }

            });




                }




            private void showDateDialog2() {

                Calendar newCalendar = Calendar.getInstance();


                datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

                        Calendar newDate = Calendar.getInstance();
                        newDate.set(year, monthOfYear, dayOfMonth);

                        res.setText(dateFormatter.format(newDate.getTime()));
                    }

                },newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));

                datePickerDialog.show();

                if(tvDateResult !=null || res !=null) {
                    try {
                        String a = tvDateResult.getText().toString();
                        String b = res.getText().toString();

                        Date aa = dateFormatter.parse(a);
                        Date bb = dateFormatter.parse(b);

                        long diff = bb.getTime() - aa.getTime() / 24 * 60 * 60 * 1000;


                        ress.setText((int) TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS));


                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }
             }

        private void showDateDialog(){


            Calendar newCalendar = Calendar.getInstance();


            datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {

                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

                    Calendar newDate = Calendar.getInstance();
                    newDate.set(year, monthOfYear, dayOfMonth);

                    tvDateResult.setText(dateFormatter.format(newDate.getTime()));
                }

            },newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));

            datePickerDialog.show();
}
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Sitgmag
  • 1
  • 1
  • I have posted an answer. Please comment if you have any issues. If it is working, please accept & upvote the answer. – Abhimanyu Jun 17 '20 at 19:25
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use java.time, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Jun 17 '20 at 19:42

1 Answers1

0
  1. Declare 2 global variables:

    Calendar date1 = null, date2 = null;

  2. In onDateSet method of both date pickers, save the selected date in global variables.

In onDateSet of first date picker,

Calendar newDate = Calendar.getInstance();
date1 = newDate
updateTextView()

Similarly, In onDateSet of second date picker,

Calendar newDate = Calendar.getInstance();
date1 = newDate
updateTextView()
  1. Call a method from both onDateSet methods.

    private void updateTextView() {
        if(date1 != null && date2 != null) {
            tvDateResult .setText(dateFormatter.format(daysBetween(date1,date2)));
        }
    }
    
    public static long daysBetween(Calendar startDate, Calendar endDate) {
    
        // Make sure we don't change the parameter passed
        Calendar newStart = Calendar.getInstance();
        newStart.setTimeInMillis(startDate.getTimeInMillis());
        newStart.set(Calendar.HOUR_OF_DAY, 0);
        newStart.set(Calendar.MINUTE, 0);
        newStart.set(Calendar.SECOND, 0);
        newStart.set(Calendar.MILLISECOND, 0);
    
        Calendar newEnd = Calendar.getInstance();
        newEnd.setTimeInMillis(endDate.getTimeInMillis());
        newEnd.set(Calendar.HOUR_OF_DAY, 0);
        newEnd.set(Calendar.MINUTE, 0);
        newEnd.set(Calendar.SECOND, 0);
        newEnd.set(Calendar.MILLISECOND, 0);
    
        long end = newEnd.getTimeInMillis();
        long start = newStart.getTimeInMillis();
        return TimeUnit.MILLISECONDS.toDays(Math.abs(end - start));
    }
    

Reference: How to get number of days between two calendar instance?

Abhimanyu
  • 11,351
  • 7
  • 51
  • 121