【问题标题】:Difference between current date and entered date in androidandroid中当前日期和输入日期之间的差异
【发布时间】:2016-09-28 06:45:57
【问题描述】:

我在这里找到输入日期和当前日期之间的差异。我的意思是我试图找到两个日期之间的天数。如果两个日期都在同一个月份,如果日期在不同月份,我得到正确的值,则差异值不正确。我不知道如何纠正这个问题。

谁能帮我弄到这个?

这是我的代码:

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

这里输入的日期是“08/24/2016”,当前日期是“09/28/2016”。结果我得到 4,这是不正确的。我想我必须使用日历或其他东西。但没有找到确切的方法来做那个..

【问题讨论】:

  • 我正在使用这种格式 mm/dd/yyyy ..我没有使用 hh:mm:ss..但是在那个链接中它是根据小时计算天数..我没有使用 hh :mm:ss.

标签: android date android-calendar


【解决方案1】:

根据documentation,字母m 被理解为小时中的分钟。要引用月份,您需要使用大写字母M

尝试将您的SimpleDateFormat 更改为:

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

【讨论】:

    【解决方案2】:

    不要使用日期来执行此操作。尝试像这样使用日历。

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

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题。

      改变

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

      获取不同月份的日期差异

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-22
        • 1970-01-01
        • 1970-01-01
        • 2019-04-24
        • 1970-01-01
        • 2019-04-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多