2

i already tried to compareTo two date in android studio like this

        String dtThen = "13-10-2013";
        Date dateThen = formatter.parse(dtThen);

        String dtNow = "15-10-2013";
        Date dateNow = formatter.parse(dtNow);

        if (dateThen.compareTo(dateNow)==0) {

            Log.d("if different date is 0");

        } else if (dateThen.compareTo(dateNow)==1) {

            Log.d("if different date is 1");


        } else if (dateThen.compareTo(dateNow)>1) {

            Log.d("if different date is 2");

        }

in my code are three condition, i want make condition compare date is 0 , 1 and greater than 1

and if compareTo is 0 is work, but how to make compareTo same with 1 and greater than 1

how ??

Megi Fernanda
  • 263
  • 1
  • 4
  • 15

4 Answers4

2

You can also create the timeStamp from your date and compare that timeStamp.
For creating the date into timeStamp below is the example

String dtThen ="13-10-2013";
String dtNow = "15-10-2013";
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
Date dateThen = (Date)formatter.parse(dtThen ); 
Date dateNow  = (Date)formatter.parse(dtNow ); 

And now u can compare the timeStamp as u want to like below

  if(dateThen.getTime()>dateNow.getTime())
   {
      //      Do some thing here
   }
   else
   {
     //Do other stuff here
   }

For getting the difference between the date, than u need to use this following code:-

long diff = dateThen.getTime() - dateNow.getTime();
long seconds = diff / 1000;
long minutes = seconds / 60;
long hours = minutes / 60;
long days = hours / 24;
System.out.println("YOUR DAY HOUR AND MINUTE DIFFERENCE==>>> "+ days +" "+hours +"  "+minutes +"  "+seconds);

And at the last day difference u can count as follow

if (days ==0) {

    Log.d("if different date is 0");

} else if (days ==1) {

    Log.d("if different date is 1");


} else if (days >1) {

    Log.d("if different date is 2");

}
Ravindra Kushwaha
  • 7,846
  • 14
  • 53
  • 103
0

You can use the below following methods to get the day difference.

public static int getDateDifference(String dtThen, String dtNow, String format) {

    // Converting String to Date
    Date dateThen = convertStingToDate(dtThen, format);
    Date dateNow = convertStingToDate(dtNow, format);

    // converting Date to Calendar object
    Calendar now = Calendar.getInstance();
    now.setTime(dateNow);
    Calendar theDay = Calendar.getInstance();
    theDay.setTime(dateThen);

    // Getting the difference in milliseconds
    long diffMillis = now.getTimeInMillis() - theDay.getTimeInMillis();

    // Converting milliseconds into number of days
    int daysDiff = (int) (diffMillis / (1000 * 60 * 60 * 24));

    return daysDiff;
}

public static Date convertStingToDate(String inputDate, String format) {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format, Locale.getDefault());

    Date date = null;
    try {
        date = simpleDateFormat.parse(inputDate);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return date;
}

you can call the above method like this from your code,

    String dtThen = "13-10-2012";
    String dtNow = "15-10-2013";
    String format = "dd-MM-yyyy";

    int daysDiff = getDateDifference(dtThen, dtNow, format);

    Log.d(TAG, "Day difference: " + daysDiff);

It will give exact day difference.

Muthukrishnan Rajendran
  • 11,122
  • 3
  • 31
  • 41
0

Here is Code:

Date d2 = null;
                Date d1 = null;
                Calendar cal = Calendar.getInstance();
                try {
                    d2 = formatter.parse(formatter.format(c.getTime()));
                    d1 = formatter.parse(preferance.getDate());//Returns 15/10/2012
                } catch (java.text.ParseException e) {
                    e.printStackTrace();
                }
                int diffInDays = (int) ((d1.getTime() - d2.getTime()) / (1000 * 60 * 60 * 24));
                //Toast.makeText(getActivity(), "diff"+diffInDays, Toast.LENGTH_SHORT).show();
                Log.d(TAG, "initWheel3: " + diffInDays);
                System.out.println(diffInDays);
Dhara Patel
  • 359
  • 5
  • 19
0
//Given you are using a valid parser

     long logTheDifferenceOfDays(Date d1, Date d2) {
                long diff = d2.getTime() - d1.getTime();
                if (diff ==0) {
                   Log.d("if different date is 0");
                } else if(diff==86400*1000||diff==-86400*1000){
                   Log.d("if different date is 1");
                } else {
                   Log.d("if different date more than one");
                }

            }
erluxman
  • 18,155
  • 20
  • 92
  • 126