0
DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener(){
    @Override
    public void onDateSet(DatePicker view,int year,int monthOfYear,int dayOfMonth){
        //Use Datepicker to pick the date i want.
        String date1 = dayOfMonth+"/"+(monthOfYear+1)+"/"+year;
        //get the current date.
        String date2 = new SimpleDateFormat("dd/MM/yyyy").format(new Date());

        Date d1 = new Date(date1);
        Date d2 = new Date(date2);

        Calendar cal1 = Calendar.getInstance();cal1.setTime(d1);
        Calendar cal2 = Calendar.getInstance();cal2.setTime(d2);

            //Compare two date.
            long calq = (d2.getTime() - d1.getTime()) / (24 * 60 * 60 * 1000);
            long caq = calq / 30;

            String c = Long.toString(calq);
            String b = Long.toString(caq);
            e1.setText(c);
            test1.setText(b + " month");


    }
};

The code is actually work,but the problem is when i select the date again,the oldest remaining month will + the latest remaining month.Can anyone tell me what the problem is??

Kai SHeng
  • 51
  • 1
  • 1
  • 4

1 Answers1

0

tl;dr

Period.between(
    LocalDate.now() ,
    LocalDate.of( 
        myDatePicker.getYear() , 
        myDatePicker.getMonth() , 
        myDatePicker.getDayOfMonth() 
    )
)

Details

You Question is not clear, but it seems you are trying to calculate the elapsed time between the chosen date and the current date (today).

Avoid legacy date-time classes

Do no use the troublesome legacy date-time classes such as java.util.Date and java.util.Calendar. These are now legacy, supplanted by the java.time classes. Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.

Also, Date and Calendar represent a date with a time-of-day. You need a date-only for your purposes.

LocalDate

The LocalDate class represents a date-only value without time-of-day and without time zone.

Time zone

A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.

You can either use the current default time zone, or, if important, ask the user for their intended/expected time zone.

Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId z = ZoneId.systemDefault();  // or ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );

Do not bother with constructing strings to build a date. Simply ask the DatePicker object for its data.

int year = myDatePicker.getYear() ;
int month = myDatePicker.getMonth() ;
int dayOfMonth = myDatePicker.getDayOfMonth() ;

Make a LocalDate from those.

LocalDate ld = LocalDate.of( year , month , dayOfMonth ) ;

Period

To represent the span of time between the chosen date and today, use the Period class.

Period period = Period.between( today , ld );  

Generating String as output

To see that value as a string in standard ISO 8601 format, call toString. You will result such a PT3D for three days. If your users can be trained to understand this compact format, consider using it.

To build your own string as output, interrogate for the number of days, months, and years in the Period object.

int days = period.getDays();
int days = period.getMonths();
int years = period.getYears();

String output = "years: " + years + "months: " + months + "days: " + days ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154