首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >android中当前日期与输入日期之间的差异

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

Stack Overflow用户
提问于 2016-09-28 06:45:57
回答 3查看 817关注 0票数 1

在这里,我发现输入日期和当前日期之间的差异。我是说,我想找出两次约会之间的日期。如果两个日期都在同一个月,则如果日期在不同月份,则得到正确的值,差异值不正确。我不知道该怎么纠正这个问题。

有人能帮我拿这个吗?

这是我的密码:

代码语言:javascript
运行
复制
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);
    }
  }
}

此处输入的日期为“2016年8月24日”,当前日期为"09/28/2016“。我得到的结果是4,这是不正确的。我想我必须用日历或者something.but来做这件事,没有得到正确的方法。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-09-28 07:29:40

根据文档的说法,字母m被理解为每小时一分钟。为了引用月份,您需要使用大写M

尝试将您的SimpleDateFormat更改为:

代码语言:javascript
运行
复制
SimpleDateFormat dates = new SimpleDateFormat("MM/dd/yyyy");
票数 2
EN

Stack Overflow用户

发布于 2016-09-28 09:18:04

不要像这样使用日期来做this.Try来使用日历。

代码语言:javascript
运行
复制
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);
}
票数 1
EN

Stack Overflow用户

发布于 2017-05-29 10:11:04

我也有过同样的问题。

变化

代码语言:javascript
运行
复制
return c1.get(Calendar.DAY_OF_MONTH) - c2.get(Calendar.DAY_OF_MONTH);

获得不同月份日期的差异

代码语言:javascript
运行
复制
return c1.get(Calendar.DAY_OF_YEAR) - c2.get(Calendar.DAY_OF_YEAR);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39739900

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档