-4

Is there any way to subtract two dates from picker.

Example:

  • I will select a date from picker1 and date from picker2.
  • Then, picker2 - picker1 to get the number of days between these two dates.
    20/10/2014
(-) 06/10/2014 
--------------
            14 days
Mahonri Moriancumer
  • 5,993
  • 2
  • 18
  • 28
uday
  • 1
  • 4
  • What have you tried, meaning.... What code have you written,and where are you stuck ? You are getting really close to this question getting knocked off. – Siddharth Jun 18 '14 at 15:09
  • see below code @Siddharth and pls help me how to subtract et and et1 – uday Jun 20 '14 at 17:32
  • uday, edit your question above\ – Siddharth Jun 21 '14 at 02:23
  • i mean am posted one code seen below – uday Jun 21 '14 at 17:35
  • Uday, you posted your question as a answer. Its against the rules of "keeping things neat and useful in StackOverflow". You need to edit your question, delete your answer (since it is NOT a answer, its actually part of the question). Now, I dont know how much more clearer I can be. Please read the FAQ. – Siddharth Jun 22 '14 at 05:50

3 Answers3

2
DateTimeUtils obj = new DateTimeUtils();
  SimpleDateFormat simpleDateFormat = 
            new SimpleDateFormat("dd/M/yyyy hh:mm:ss");

  try {

    Date date1 = simpleDateFormat.parse("10/10/2013 11:30:10");
    Date date2 = simpleDateFormat.parse("13/10/2013 20:35:55");

    obj.printDifference(date1, date2);

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

}

//1 minute = 60 seconds
//1 hour = 60 x 60 = 3600
//1 day = 3600 x 24 = 86400
public void printDifference(Date startDate, Date endDate){

    //milliseconds
    long different = endDate.getTime() - startDate.getTime();

    System.out.println("startDate : " + startDate);
    System.out.println("endDate : "+ endDate);
    System.out.println("different : " + different);

    long secondsInMilli = 1000;
    long minutesInMilli = secondsInMilli * 60;
    long hoursInMilli = minutesInMilli * 60;
    long daysInMilli = hoursInMilli * 24;

    long elapsedDays = different / daysInMilli;
    different = different % daysInMilli;

    long elapsedHours = different / hoursInMilli;
    different = different % hoursInMilli;

    long elapsedMinutes = different / minutesInMilli;
    different = different % minutesInMilli;

    long elapsedSeconds = different / secondsInMilli;

    System.out.printf(
        "%d days, %d hours, %d minutes, %d seconds%n", 
        elapsedDays,
        elapsedHours, elapsedMinutes, elapsedSeconds);

}

output is this

startDate : Thu Oct 10 11:30:10 SGT 2013
endDate : Sun Oct 13 20:35:55 SGT 2013
different : 291945000
3 days, 9 hours, 5 minutes, 45 seconds

Answer from this link Android difference between Two Dates

Community
  • 1
  • 1
Rohit
  • 3,401
  • 4
  • 33
  • 60
1

Since not much information is provided, I assume you are using DatePicker

The basic flow is like that:

by calling getDayOfMonth(), getMonth() and getYear() from DataPicker to obtain day, month and year respectively.

after obtaining those information, I think you should be able to calculate the day difference between them.

Just for your reference, the following is a topic about day difference, hope this help.

Difference in days between two dates in Java?

Community
  • 1
  • 1
hungr
  • 2,086
  • 1
  • 20
  • 33
1

short and simple

SimpleDateFormat simpleDateFormat= new SimpleDateFormat("dd/MM/yyyy");
java.util.Date date1 = null;
java.util.Date date2 = null;
try {
        date1  = simpleDateFormat.parse("20/10/2014");
        date2  = simpleDateFormat.parse("06/10/2014");
    } catch (java.text.ParseException e) {
        e.printStackTrace();
    }
int diffInDays = (int) ((date1.getTime() - date2.getTime())/ (1000 * 60 * 60 * 24));

EDIT :

Hello Uday

it is Very Simple you have two EditText so first Set your date into et and et1 and now

et.getText().toString(); //Do same for et1 also 

this mothod will helps you for getting date and now apply to my code

MilapTank
  • 9,988
  • 7
  • 38
  • 53