如何在 android studio 中比较或计算两个日期的差异

How to compareTo or Count Difference from two date in android studio

我已经尝试在 android 工作室中像这样比较两个日期

        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");

        }

在我的代码中有三个条件,我希望条件比较日期是 0、1 和大于 1

如果 compareTo 为 0 是可行的,但是如何使 compareTo 与 1 相同且大于 1

如何 ??

您还可以根据日期创建时间戳并比较该时间戳。
下面是将日期创建到时间戳中的示例

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 ); 

现在你可以比较你想要的时间戳如下

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

要获取日期之间的差异,您需要使用以下代码:-

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);

最后 差异你可以算作跟随

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");

}

您可以使用以下方法获取日差。

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;
}

您可以从您的代码中像这样调用上述方法,

    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);

它会给出准确的日差。

代码如下:

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);
//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");
                }

            }