0

i have in my android app a date picker. i would like to get the diff days between the selected date and the Date of Today.

my Result:

E/-->: DatePicker: 1448798400691   // 29.11.2015
E/-->: NOW: 1448920633692
E/-->: DIFF: -1
E/-->: DatePicker: 1448884800191   // 30.11.2015
E/-->: NOW: 1448920637192
E/-->: DIFF: 0
E/-->: DatePicker: 1448971200451   // 01.12.2015
E/-->: NOW: 1448920644463
E/-->: DIFF: 0
Trombone0904
  • 4,132
  • 8
  • 51
  • 104

5 Answers5

1

You can calculate the difference as follows. to get dates use the following code.

//to get date from time picker

Calendar calendar = Calendar.getInstance();
calendar.set(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth(), 0, 0, 0);        //use object of Datepicker widget

long diff = calendar.getTimeInMillis() - System.currentTimeMillis();       //calculate difference
long seconds = TimeUnit.MILLISECONDS.toSeconds(diff);        //here you will have diff in seconds.
long daysdiff = TimeUnit.MILLISECONDS.toDays(diff);          //if you  want diff in days
karan
  • 8,637
  • 3
  • 41
  • 78
1

you should try this

SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
Date past = format.parse("12/10/2015 18:10:52");
Date now = new Date();
System.out.println(TimeUnit.MILLISECONDS.toSeconds(now.getTime() - past.getTime()) + " seconds ago");
System.out.println(TimeUnit.MILLISECONDS.toMinutes(now.getTime() - past.getTime()) + " minutes ago");
System.out.println(TimeUnit.MILLISECONDS.toHours(now.getTime() - past.getTime()) + " hours ago");
System.out.println(TimeUnit.MILLISECONDS.toDays(now.getTime() - past.getTime()) + " days ago");

you can pass your selected date in format.parse("") in given format

Ravi
  • 34,851
  • 21
  • 122
  • 183
0

you can use this method to get difference between two dates.

  public static int getDateDifference(long startDate, long endDate) {

    //milliseconds
    long different = endDate - startDate;

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

    long elapsedDays = different / daysInMilli;
    return (int) elapsedDays;
}
droidev
  • 7,352
  • 11
  • 62
  • 94
0

Try this

public static long printDifference(Date startDate, Date endDate){

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

    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;

    return elapsedDays;
}
Narendra Motwani
  • 1,085
  • 10
  • 20
0

try this example to solve the

    String dateStart = "11/28/2015 09:29:58";
        String dateStop = "11/29/2015 10:31:48";

        //HH converts hour in 24 hours format (0-23), day calculation
        SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

        Date d1 = null;
        Date d2 = null;

        try {
            d1 = format.parse(dateStart);
            d2 = format.parse(dateStop);

            //in milliseconds
            long diff = d2.getTime() - d1.getTime();

            long diffSeconds = diff / 1000 % 60;
            long diffMinutes = diff / (60 * 1000) % 60;
            long diffHours = diff / (60 * 60 * 1000) % 24;
            long diffDays = diff / (24 * 60 * 60 * 1000);
   }
  catch(Exception e){}
Hemina
  • 375
  • 1
  • 11