2016-09-28 127 views
1

這裏我找到輸入日期和當前日期之間的差異。我的意思是我試圖找到兩個日期之間的天數。如果兩個日期都在同一個月內,如果日期在不同的月份,我會得到正確的值,但差異值不正確。我沒有得到如何糾正這一問題。Android中當前日期和輸入日期之間的差異

任何人都可以幫助我得到這個嗎?

這是我的代碼:

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日」和當前日期是「2016年9月28日」。我得到4結果是不正確的。我認爲我必須使用日曆或東西,但沒有得到確切的方式來做那個..

+0

看到這個鏈接http://stackoverflow.com/questions/21285161/android-difference-between-two-dates –

+0

我使用這種格式mm/dd/yyyy ..並且我沒有使用hh:mm:ss ..但是在那個鏈接中,它計算基於小時數的天..我沒有使用hh:mm:ss。 –

回答

2

根據documentationm被理解爲小時分鐘。爲了引用月份,您需要使用大寫M

嘗試改變你的SimpleDateFormat這樣:

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

不要使用日期做this.Try使用日曆這樣。

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

將調查它.. –

0

我有同樣的問題。

變化

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); 
相關問題