Android編程實現(xiàn)根據(jù)不同日期計算天數(shù)差的方法
本文實例講述了Android編程實現(xiàn)根據(jù)不同日期計算天數(shù)差的方法。分享給大家供大家參考,具體如下:
Calendar cal1 = getCalendarFromDate(mStartDate); long startTime = cal1.getTimeInMillis(); Calendar cal2 = getCalendarFromDate(mEndDate); long endTime = cal2.getTimeInMillis(); int numberOfDays = (int)(endTime - startTime)/(24 * 60 * 60 * 1000); /** * @param date format is 2012-9-18 * @return Calendar value is after set date's value */ private Calendar getCalendarFromDate(final String date) { int year = 0; int month = 0; int day = 0; try { String[] array = date.split("-"); int[] arrayInt = new int[array.length]; for (int i = 0; i < array.length; i++) { arrayInt[i] = Integer.parseInt(array[i]); if(i == 0) { year = arrayInt[0]; } else if(i == 1){ month = arrayInt[1]; } else if(i == 2){ day = arrayInt[2]; } } } catch (Exception e) { e.printStackTrace(); } Calendar cal = Calendar.getInstance(); if(year > 0 && month >= 0 && day >= 0) { cal.set(year, month, day); } return cal; }
PS:month 取值范圍為0-11
更多關于Android相關內(nèi)容感興趣的讀者可查看本站專題:《Android調(diào)試技巧與常見問題解決方法匯總》、《Android開發(fā)入門與進階教程》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》及《Android控件用法總結》
希望本文所述對大家Android程序設計有所幫助。
相關文章
詳解AndroidStudio中代碼重構菜單Refactor功能
這篇文章主要介紹了AndroidStudio中代碼重構菜單Refactor功能詳解,本文通過代碼演示,功能截圖來詳細說明as為大名重構提供的各項功能,需要的朋友可以參考下2019-11-11android自定義popupwindow仿微信右上角彈出菜單效果
這篇文章主要為大家詳細介紹了android自定義popupwindow仿微信右上角彈出菜單效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11