-1

I need to compute the days between two dates. However, the format of dates from text view are different from SimpleDateFormat. For example, after picking date from the dialog, the date that will display is April 22, 2019. However, I cannot compute the days since the format of SimpleDateFormat is "MM dd yyyy" so it should AP 22 2019.

Codes

Details.java

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            l[0]= System.currentTimeMillis();
            datePicker.show(getSupportFragmentManager(),l[0]+"");
            startDateOrEndDate = true ;
        }
    });
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            datePicker.show(getSupportFragmentManager(),l[0]+"");
            startDateOrEndDate = false ;

            //SimpleDateFormat myFormat = new SimpleDateFormat("MM dd yyyy");
            SimpleDateFormat myFormat = new SimpleDateFormat("MM  dd yyyy");
            DateFormat.getDateInstance(DateFormat.LONG);
            String firstDate=chckIn.getText().toString();
            Date startDate = null;   // initialize start date
            String secondDate=chckOut.getText().toString();
            Date endDate   = null; // initialize  end date
            try {
                startDate = myFormat.parse(firstDate);
                endDate = myFormat.parse(secondDate);

                long duration  = endDate.getTime() - startDate.getTime();
                long diffInDays = TimeUnit.MILLISECONDS.toDays(duration);

                String diffInDaysString = String.valueOf(diffInDays);

                beach_days.setText(diffInDaysString);

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

        }

onDateSet

public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
    Calendar c = Calendar.getInstance();
    c.set(Calendar.YEAR, year);
    c.set(Calendar.MONTH, month);
    c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
    l[0]= c.getTimeInMillis()+24*60*60*1000;
    String date = DateFormat.getDateInstance(DateFormat.LONG).format(c.getTime());
    System.out.println(date);

    /*MM dd yyyy*/
    TextView textView = (TextView) findViewById(R.id.checkInTV);
    TextView textView2 = (TextView) findViewById(R.id.checkOutTV);

    if (startDateOrEndDate) {
        textView.setText(date);
    } else {
        textView2.setText(date);
    }

}

Screenshot of Date from Date Picker Dialog

Reymand
  • 41
  • 1
  • 7

1 Answers1

0

Found a solution: SimpleDateFormat myFormat = new SimpleDateFormat("MMMM dd, yyyy", Locale.ENGLISH);

Thanks

Reymand
  • 41
  • 1
  • 7