Android Day Compare getDaysAgo(String fromDay, int toOlderDay, int toOlderDayMonth)

Here you can find the source of getDaysAgo(String fromDay, int toOlderDay, int toOlderDayMonth)

Description

get Days Ago

Declaration

public static int getDaysAgo(String fromDay, int toOlderDay,
            int toOlderDayMonth) 

Method Source Code

//package com.java2s;

public class Main {
    public static int getDaysAgo(String fromDay, int toOlderDay,
            int toOlderDayMonth) { //a bit dom
        int daysAgo = 0;
        char charAt0 = fromDay.charAt(0);
        char charAt1 = fromDay.charAt(1);
        String sDay = "" + charAt0 + charAt1;
        sDay = sDay.trim();//from w w w  . j av a2  s .  c  om
        int fromDayInt = Integer.valueOf(sDay);

        if (fromDayInt >= toOlderDay) { //this means that both dates are still in the same month
            return fromDayInt - toOlderDay; // return 0 or number not less more than from
        } else { // deal with feb some other day
            for (int i = 0; i < 13; i++) { //13 is the number of month.
                int toMonthEndDaysLeft;
                switch (toOlderDayMonth) {
                case 0: //jan ends with 31
                    toMonthEndDaysLeft = 31 - toOlderDay;
                    daysAgo = fromDayInt + toMonthEndDaysLeft;
                    break;
                case 1: //feb
                    toMonthEndDaysLeft = 28 - toOlderDay;
                    daysAgo = fromDayInt + toMonthEndDaysLeft;
                    break;
                case 2: //march
                    toMonthEndDaysLeft = 31 - toOlderDay;
                    daysAgo = fromDayInt + toMonthEndDaysLeft;
                    break;
                case 3:
                    toMonthEndDaysLeft = 31 - toOlderDay;
                    daysAgo = fromDayInt + toMonthEndDaysLeft;
                    break;
                case 4:
                    toMonthEndDaysLeft = 30 - toOlderDay;
                    daysAgo = fromDayInt + toMonthEndDaysLeft;
                    break;
                case 5: //may
                    toMonthEndDaysLeft = 31 - toOlderDay;
                    daysAgo = fromDayInt + toMonthEndDaysLeft;
                    break;
                case 6: //june
                    toMonthEndDaysLeft = 30 - toOlderDay;
                    daysAgo = fromDayInt + toMonthEndDaysLeft;
                    break;
                case 7:
                    toMonthEndDaysLeft = 31 - toOlderDay;
                    daysAgo = fromDayInt + toMonthEndDaysLeft;
                    break;
                case 8: //august
                    toMonthEndDaysLeft = 31 - toOlderDay;
                    daysAgo = fromDayInt + toMonthEndDaysLeft;
                    break;
                case 9:
                    toMonthEndDaysLeft = 30 - toOlderDay;
                    daysAgo = fromDayInt + toMonthEndDaysLeft;
                    break;
                case 10:
                    toMonthEndDaysLeft = 31 - toOlderDay;
                    daysAgo = fromDayInt + toMonthEndDaysLeft;
                    break;
                case 11:
                    toMonthEndDaysLeft = 30 - toOlderDay;
                    daysAgo = fromDayInt + toMonthEndDaysLeft;
                    break;
                case 12:
                    toMonthEndDaysLeft = 31 - toOlderDay;
                    daysAgo = fromDayInt + toMonthEndDaysLeft;
                    break;
                }
            }
        }
        return daysAgo;
    }
}

Related

  1. isSameDay(Date date1, Date date2)
  2. isSameDay(Calendar cal1, Calendar cal2)
  3. daysSince(long date)
  4. isDateDayEqual(long lTime1, long lTime2)
  5. isDayChanged(long lastScan)