spykeburn
spykeburn

Reputation: 163

Date picker to Days conversion

I am trying to convert a date from date picker to get number of days in Android. For example if I choose 19/11/2015, the number of days should be 2 days. I need to find a solution for this as it is important for my project. There are many solutions on the web for getting the number of days between two dates but not from a single date. Can anyone help please?

Below is the method where I have to set and convert the date.

public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
    String date = +dayOfMonth+"/"+(++monthOfYear)+"/"+year;

    Calendar end = Calendar.getInstance();
    end.set(year, monthOfYear, dayOfMonth);
    long timenow= System.currentTimeMillis();


    long endDate = end.getTimeInMillis();

    long diffTime = endDate - timenow;
   days = (int) (diffTime / (1000 * 60 * 60 * 24));
    DateFormat dateFormat = DateFormat.getDateInstance();
    dateFormat.format(endDate);
    //System.out.println(diffDays);


    dateTextView.setText(Integer.toString(days));

Upvotes: 0

Views: 164

Answers (1)

Swagata Acharyya
Swagata Acharyya

Reputation: 657

Answer for:

Right now it is giving me this bug

The reason for the error is you are trying to set an int as value to dateTextView.

Instead of dateTextView.setText(days); try dateTextView.setText(Integer.toString(days));

Upvotes: 1

Related Questions