简体   繁体   English

当前日期与Android中输入日期之间的差异

[英]Difference between current date and entered date in android

Here I am finding the difference between the entered date and current date. 在这里,我发现了输入日期和当前日期之间的差异。 I mean I am trying to find the no of days between two Dates. 我的意思是我试图找到两个日期之间的天数。 If both the dates are in the same month, I am getting correct value if the dates are in different months, the difference value is incorrect. 如果两个日期都在同一个月,那么如果两个日期都在不同月份,则我得到正确的值,则差值不正确。 I am not getting how to rectify this one. 我没有得到如何纠正这一问题。

can anyone pls help me get this one? 谁能帮助我得到这个?

This is my code: 这是我的代码:

public class MyService extends Service {

  String dayDifference;

  NotificationManager manager;
  Notification myNotication;

  @Override public IBinder onBind(Intent intent) {
    return null;
  }//End of onBind method

  public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);
    manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    try {

      //Dates to compare
      String CurrentDate;
      String FinalDate = "08/24/2016";

      Date date1 = new Date();
      Date date2;

      SimpleDateFormat dates = new SimpleDateFormat("mm/dd/yyyy");

      //Setting dates
      date1.setTime(System.currentTimeMillis());
      CurrentDate = dates.format(date1);
      date2 = dates.parse(FinalDate);
      date1 = dates.parse(CurrentDate);

      //Comparing dates
      long difference = Math.abs(date1.getTime() - date2.getTime());
      long differenceDates = difference / (24 * 60 * 60 * 1000);

      //Convert long to String
      dayDifference = Long.toString(differenceDates);

      Log.e("HERE.................", "HERE: " + dayDifference);
      System.out.println("..............difference date " + dayDifference);

      Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
      Intent targetIntent = new Intent(this, Mnst.class);

      PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, targetIntent, 0);
      NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

      builder.setAutoCancel(true);
      builder.setTicker("Predictor");
      builder.setContentTitle("Miisky Notification");

      builder.setContentText(
          "You have " + dayDifference + " days left. Click on notification to know more..");
      builder.setSmallIcon(R.drawable.miilogo);
      builder.setSound(soundUri);
      builder.setContentIntent(pendingIntent);
      builder.setOngoing(true);
      //builder.setSubText("Your vault number is " + s.vault_no);   //API level 16
      builder.setNumber(100);

      builder.build();

      //noinspection deprecation
      myNotication = builder.getNotification();
      builder.setAutoCancel(true);
      manager.notify(11, myNotication);

      Toast.makeText(getApplicationContext(), " " + dayDifference + " Days Left ",
          Toast.LENGTH_LONG).show();
    } catch (Exception exception) {
      Log.e("DIDN'T WORK............", "exception " + exception);
    }
  }
}

Here the entered date is "08/24/2016" and the current date is "09/28/2016". 此处输入的日期为“ 08/24/2016”,当前日期为“ 09/28/2016”。 I am getting 4 as the result which is incorrect. 我得到4作为不正确的结果。 I think I have to use Calendar or something.but not getting exact way to do that one.. 我想我必须使用Calendar或其他方法。但是没有确切的方法来执行该操作。

According to documentation the letter m is understood as minute in hour. 根据文档 ,字母m被理解为分钟。 In order to refer to month you need to use capital M . 为了引用月份,您需要使用大写M

Try to change your SimpleDateFormat to this: 尝试将您的SimpleDateFormat更改为此:

SimpleDateFormat dates = new SimpleDateFormat("MM/dd/yyyy");

Do not use date to do this.Try to use Calendar like this. 不要使用日期来执行此操作。尝试使用这样的日历。

public static final int diffOfDay(Date d1,Date d2){
    Calendar c1 = Calendar.getInstance();
    c1.setTime(d1);

    Calendar c2 = Calendar.getInstance();
    c2.setTime(d2);

    return c1.get(Calendar.DAY_OF_MONTH) - c2.get(Calendar.DAY_OF_MONTH);
}

I had the same issue. 我遇到过同样的问题。

change 更改

return c1.get(Calendar.DAY_OF_MONTH) - c2.get(Calendar.DAY_OF_MONTH);

to get difference for date with different months 得到不同月份的日期差异

return c1.get(Calendar.DAY_OF_YEAR) - c2.get(Calendar.DAY_OF_YEAR);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM