Android Utililty Methods Date Interval Get

List of utility methods to do Date Interval Get

Description

The list of methods to do Date Interval Get are organized into topic(s).

Method

longgetDiff(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) {
...
longgetDiff(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) {
...
intgetDiffDays(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;
intgetDiffDays(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;
intgetDiffHour(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) {
...
intgetDiffHour(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) {
...
longgetFragment(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)
...
longgetFragment(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);
longgetFragmentInDays(Date date, int fragment)

Returns the number of days within the fragment.

return getFragment(date, fragment, Calendar.DAY_OF_YEAR);
longgetFragmentInHours(Date date, int fragment)

Returns the number of hours within the fragment.

return getFragment(date, fragment, Calendar.HOUR_OF_DAY);