List of utility methods to do Date Interval Get
long | getDiff(String startTime, String endTime) get Diff long diff = 0; SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { Date startDate = ft.parse(startTime); Date endDate = ft.parse(endTime); diff = startDate.getTime() - endDate.getTime(); diff = diff / 1000; } catch (ParseException e) { ... |
long | getDiff(String startTime, String endTime) get Diff long diff = 0; SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { Date startDate = ft.parse(startTime); Date endDate = ft.parse(endTime); diff = startDate.getTime() - endDate.getTime(); diff = diff / 1000; } catch (ParseException e) { ... |
int | getDiffDays(String startDate, String endDate) get Diff Days long diff = 0; SimpleDateFormat ft = null; if (startDate.indexOf("/") > 0 && endDate.indexOf("/") > 0) { ft = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); if (startDate.indexOf("-") > 0 && endDate.indexOf("-") > 0) { ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { Date sDate = ft.parse(startDate + " 00:00:00"); Date eDate = ft.parse(endDate + " 00:00:00"); diff = eDate.getTime() - sDate.getTime(); diff = diff / 86400000; } catch (ParseException e) { e.printStackTrace(); return (int) diff; |
int | getDiffDays(String startDate, String endDate) get Diff Days long diff = 0; SimpleDateFormat ft = null; if (startDate.indexOf("/") > 0 && endDate.indexOf("/") > 0) { ft = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); if (startDate.indexOf("-") > 0 && endDate.indexOf("-") > 0) { ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { Date sDate = ft.parse(startDate + " 00:00:00"); Date eDate = ft.parse(endDate + " 00:00:00"); diff = eDate.getTime() - sDate.getTime(); diff = diff / 86400000; } catch (ParseException e) { e.printStackTrace(); return (int) diff; |
int | getDiffHour(String startTime, String endTime) get Diff Hour long diff = 0; SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { Date startDate = ft.parse(startTime); Date endDate = ft.parse(endTime); diff = startDate.getTime() - endDate.getTime(); diff = diff / (1000 * 60 * 60); } catch (ParseException e) { ... |
int | getDiffHour(String startTime, String endTime) get Diff Hour long diff = 0; SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { Date startDate = ft.parse(startTime); Date endDate = ft.parse(endTime); diff = startDate.getTime() - endDate.getTime(); diff = diff / (1000 * 60 * 60); } catch (ParseException e) { ... |
long | getFragment(Calendar calendar, int fragment, int unit) Calendar-version for fragment-calculation in any unit if (calendar == null) { throw new IllegalArgumentException("The date must not be null"); long millisPerUnit = getMillisPerUnit(unit); long result = 0; switch (fragment) { case Calendar.YEAR: result += (calendar.get(Calendar.DAY_OF_YEAR) * MILLIS_PER_DAY) ... |
long | getFragment(Date date, int fragment, int unit) Date-version for fragment-calculation in any unit if (date == null) { throw new IllegalArgumentException("The date must not be null"); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return getFragment(calendar, fragment, unit); |
long | getFragmentInDays(Date date, int fragment) Returns the number of days within the fragment. return getFragment(date, fragment, Calendar.DAY_OF_YEAR);
|
long | getFragmentInHours(Date date, int fragment) Returns the number of hours within the fragment. return getFragment(date, fragment, Calendar.HOUR_OF_DAY);
|