两天之间的Android天

我想比较两个date为我的Android应用程序,但我有一个非常奇怪的问题。

例如:

如果我back in the pastdate设置在127天前:

 this.dateEvent = System.currentTimeMillis() - (127 * 24 * 3600 * 1000) 

然后将它与当前date(天之间)进行比较

  Calendar sDate = getDatePart(new Date(this.dateEvent)); Calendar eDate = getDatePart(new Date(System.currentTimeMillis())); int daysBetween = 0; while (sDate.before(eDate)) { sDate.add(Calendar.DAY_OF_MONTH, 1); daysBetween ++; } while (sDate.after(eDate)) { eDate.add(Calendar.DAY_OF_MONTH, 1); daysBetween ++; } return daysBetween; 

它将返回22,这完全不是预期的。

我做错了什么或与Calendar类课题?

Solutions Collecting From Web of "两天之间的Android天"

请参考此代码,这可能会帮助你。

 public String getCountOfDays(String createdDateString, String expireDateString) { SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault()); Date createdConvertedDate = null, expireCovertedDate = null, todayWithZeroTime = null; try { createdConvertedDate = dateFormat.parse(createdDateString); expireCovertedDate = dateFormat.parse(expireDateString); Date today = new Date(); todayWithZeroTime = dateFormat.parse(dateFormat.format(today)); } catch (ParseException e) { e.printStackTrace(); } int cYear = 0, cMonth = 0, cDay = 0; if (createdConvertedDate.after(todayWithZeroTime)) { Calendar cCal = Calendar.getInstance(); cCal.setTime(createdConvertedDate); cYear = cCal.get(Calendar.YEAR); cMonth = cCal.get(Calendar.MONTH); cDay = cCal.get(Calendar.DAY_OF_MONTH); } else { Calendar cCal = Calendar.getInstance(); cCal.setTime(todayWithZeroTime); cYear = cCal.get(Calendar.YEAR); cMonth = cCal.get(Calendar.MONTH); cDay = cCal.get(Calendar.DAY_OF_MONTH); } /*Calendar todayCal = Calendar.getInstance(); int todayYear = todayCal.get(Calendar.YEAR); int today = todayCal.get(Calendar.MONTH); int todayDay = todayCal.get(Calendar.DAY_OF_MONTH); */ Calendar eCal = Calendar.getInstance(); eCal.setTime(expireCovertedDate); int eYear = eCal.get(Calendar.YEAR); int eMonth = eCal.get(Calendar.MONTH); int eDay = eCal.get(Calendar.DAY_OF_MONTH); Calendar date1 = Calendar.getInstance(); Calendar date2 = Calendar.getInstance(); date1.clear(); date1.set(cYear, cMonth, cDay); date2.clear(); date2.set(eYear, eMonth, eDay); long diff = date2.getTimeInMillis() - date1.getTimeInMillis(); float dayCount = (float) diff / (24 * 60 * 60 * 1000); return ("" + (int) dayCount + " Days"); } 

这是一个双线解决scheme:

 long msDiff = Calendar.getInstance().getTimeInMillis() - testCalendar.getTimeInMillis(); long daysDiff = TimeUnit.MILLISECONDS.toDays(msDiff); 

在这个例子中,它获取date“testCalendar”和当前date之间的天数。

我终于find了解决这个问题的最简单的方法。 这是我的代码:

 public int getTimeRemaining() { Calendar sDate = toCalendar(this.dateEvent); Calendar eDate = toCalendar(System.currentTimeMillis()); // Get the represented date in milliseconds long milis1 = sDate.getTimeInMillis(); long milis2 = eDate.getTimeInMillis(); // Calculate difference in milliseconds long diff = Math.abs(milis2 - milis1); return (int)(diff / (24 * 60 * 60 * 1000)); } private Calendar toCalendar(long timestamp) { Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(timestamp); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); return calendar; } 

希望能帮助到你。

  Date userDob = new SimpleDateFormat("yyyy-MM-dd").parse(dob); Date today = new Date(); long diff = today.getTime() - userDob.getTime(); int numOfYear = (int) ((diff / (1000 * 60 * 60 * 24))/365); int numOfDays = (int) (diff / (1000 * 60 * 60 * 24)); int hours = (int) (diff / (1000 * 60 * 60)); int minutes = (int) (diff / (1000 * 60)); int seconds = (int) (diff / (1000)); 

最好的办法 :-

  long fromCalender = Calender.getInstance(); fromCalender.set...// set the from dates long toCalender = Calender.getInstance(); fromCalender.set...// set the to dates long diffmili = fromCalender - toCalender; long hours = TimeUnit.MILLISECONDS.toHours(diffmili); long days = TimeUnit.MILLISECONDS.toDays(diffmili); long min = TimeUnit.MILLISECONDS.toMinutes(diffmili); long sec = TimeUnit.MILLISECONDS.toSeconds(diffmili); 
 public long Daybetween(String date1,String date2,String pattern) { SimpleDateFormat sdf = new SimpleDateFormat(pattern,Locale.ENGLISH); Date Date1 = null,Date2 = null; try{ Date1 = sdf.parse(date1); Date2 = sdf.parse(date2); }catch(Exception e) { e.printStackTrace(); } return (Date2.getTime() - Date1.getTime())/(24*60*60*1000); } 

我有同样的需要,我最终结束了使用乔达时间,这是非常方便,并提供了许多其他function,包括你正在寻找。

你可以从这里下载文件。

一旦你将jar文件包含到你的项目中,你可以很容易的做到以下几点:

 int daysBetween = Days.daysBetween(new DateTime(sDate), new DateTime(eDate)).getDays(); 

为我工作的最佳解决scheme是:

 private static int findDaysDiff(long unixStartTime,long unixEndTime) { Calendar calendar1 = Calendar.getInstance(); calendar1.setTimeInMillis(unixStartTime); calendar1.set(Calendar.HOUR_OF_DAY, 0); calendar1.set(Calendar.MINUTE, 0); calendar1.set(Calendar.SECOND, 0); calendar1.set(Calendar.MILLISECOND, 0); Calendar calendar2 = Calendar.getInstance(); calendar2.setTimeInMillis(unixEndTime); calendar2.set(Calendar.HOUR_OF_DAY, 0); calendar2.set(Calendar.MINUTE, 0); calendar2.set(Calendar.SECOND, 0); calendar2.set(Calendar.MILLISECOND, 0); return (int) ((calendar2.getTimeInMillis()-calendar1.getTimeInMillis())/(24 * 60 * 60 * 1000)); } 

由于它首先将HourMinuteSecondMillisecond0 ,现在差异将只有几天。