-1

I getting two date from calendars.It writing into a string builder.I want to getting difference between two date also I want to keep the number of days remaining between times,except weekends.

 private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {

        // when dialog box is closed, below method will be called.
        public void onDateSet(DatePicker view, int selectedYear,  int selectedMonth, int selectedDay) {


            year = selectedYear;
            month = selectedMonth;
            day = selectedDay;

            if (cur == DATE_DIALOG_ID) {
                // set selected date into textview
               permitDate = new StringBuilder().append(day).append(".").append(month + 1).append(".").append(year).append(" ").toString();
                tvDisplayDate.setText("Date : " + permitDate);

            } else {
               startDate = new StringBuilder().append(day).append(".").append(month + 1) .append(".").append(year).append(" ").toString();
                tvDisplayDate2.setText("Date : " + startDate);


            }

        }
    };
Emre Aslan
  • 119
  • 1
  • 8
  • 1
    for such thinks you should use the calendar API of java – Jens Oct 19 '16 at 06:54
  • Can I do without using the APIs Calendar? – Emre Aslan Oct 19 '16 at 06:58
  • Don't try to reinvent the wheel. Build on existing solutions. Like AndroidThreeTenBp or Joda-time. You'll reduce the number of times you'll bash your head on the wall. – Elvis Chweya Oct 19 '16 at 06:58
  • try this http://stackoverflow.com/questions/17940200/how-to-find-the-duration-of-difference-between-two-dates-in-java – D.J Oct 19 '16 at 06:59
  • Why you want to do it without the calendar API? – Jens Oct 19 '16 at 07:00
  • I will look CalendarApi.I'm using datapicker and I getting date and I sending string value on webservice.. – Emre Aslan Oct 19 '16 at 07:02
  • Possible duplicate of [Calculating the difference between two Java date instances](http://stackoverflow.com/questions/1555262/calculating-the-difference-between-two-java-date-instances) – Simone Poggi Oct 19 '16 at 08:01

2 Answers2

0
Calendar thatDay = Calendar.getInstance();
thatDay.set(Calendar.DAY_OF_MONTH,25);
thatDay.set(Calendar.MONTH,7); // 0-11 so 1 less
thatDay.set(Calendar.YEAR, 1985);

Calendar today = Calendar.getInstance();

long diff = today.getTimeInMillis() - thatDay.getTimeInMillis(); //result in millis

long days = diff / (24 * 60 * 60 * 1000);

To Parse the date from a string, you could use

String strThatDay = "1985/08/25";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
Date d = null;
try {
 d = formatter.parse(strThatDay);//catch exception
} catch (ParseException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
} 


Calendar thatDay = Calendar.getInstance();
thatDay.setTime(d); //rest is the same....
Ganesh Pokale
  • 1,616
  • 11
  • 27
0

Use JodaTime library to find the difference between dates. For more information follow the instructions Use Joda Time.

Community
  • 1
  • 1
Javed Salat
  • 506
  • 4
  • 10
  • 26