Android Date Interval Get totalDays(Object o)

Here you can find the source of totalDays(Object o)

Description

total Days

License

Apache License

Declaration

public static int totalDays(Object o) 

Method Source Code

//License from project: Apache License 

import org.joda.time.DateTime;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;

public class Main{
    public static final String MM_DD_YYYY = "MM/dd/yyyy";
    private static final long MILLIS_OF_DAY = 24 * 60 * 60 * 1000;
    public static int totalDays(Object o) {
        DateTime dateTime = narrowDateTime(o);
        if (dateTime == null) {
            return 0;
        }/*from  w  ww  . ja v a 2 s.  c  o  m*/
        long millis = dateTime.getMillis();
        return (int) ((millis - millis % MILLIS_OF_DAY) / MILLIS_OF_DAY);
    }
    public static DateTime narrowDateTime(Object o) {

        if (o instanceof DateTime) {
            return (DateTime) o;
        }

        if (o instanceof Calendar) {
            return new DateTime(((Calendar) o).getTimeInMillis());
        }

        if (o instanceof String) {
            return parse((String) o);
        }

        if (o instanceof Date) {
            return new DateTime(((Date) o).getTime());
        }

        return null;
    }
    public static DateTime parse(String s) {

        if (Strings.isEmpty(s)) {

            return null;
        }

        try {

            SimpleDateFormat mDateFormat = new SimpleDateFormat(MM_DD_YYYY,
                    Locale.getDefault());
            mDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
            return new DateTime(mDateFormat.parse(s));
        } catch (Exception e) {

            e.printStackTrace();
        }

        return null;
    }
}

Related

  1. secondDiff(String startTime, String endTime)
  2. secondsBetween(Date dt1, Date dt2)
  3. getDateOffset()
  4. interval(Date date)
  5. checkIsIntervalDay(String startTime, String endTime)
  6. calculatorDaysAgo(String date, Locale locale, String format)
  7. getFirstInterval(Context context, long lastupdate, long updateinterval)
  8. timeDifference(Date date)
  9. getTimeRangeStr(Date startDate, Date endDate)