Android Long to Date Convert getDatesStartingFromToWithInterval( long startDate, long endDate, int dayInterval)

Here you can find the source of getDatesStartingFromToWithInterval( long startDate, long endDate, int dayInterval)

Description

Return un ArrayList of Date objects from the starting date at the end date using the interval time from a date and the next one

Parameter

Parameter Description
startDate a parameter
endDate a parameter
dayInterval a parameter

Declaration

public static ArrayList<Date> getDatesStartingFromToWithInterval(
        long startDate, long endDate, int dayInterval) 

Method Source Code

//package com.java2s;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class Main {
    /**//from w  w w. java2  s.  co m
     * Return un ArrayList of Date objects from the starting date at the end date using the interval time from a date and
     * the next one
     * 
     * @param startDate
     * @param endDate
     * @param dayInterval
     * @return
     */
    public static ArrayList<Date> getDatesStartingFromToWithInterval(
            long startDate, long endDate, int dayInterval) {
        ArrayList<Date> dates = new ArrayList<Date>();

        Date date = new Date(startDate);

        while ((date = addDays(date, dayInterval)).getTime() <= endDate) {
            dates.add(date);
        }

        return dates;
    }

    /**
     * Add days to an existent date
     * @param date
     * @param days
     * @return
     */
    public static Date addDays(Date date, int days) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, days); //minus number would decrement the days
        return cal.getTime();
    }
}

Related

  1. changeMillisToFormat(long milliseconds, String pattern)
  2. convertLongToDateString(long mills, String format)
  3. format(String pattern, long timeInMillis)
  4. formatDateD(Context paramContext, long paramLong)
  5. formatTimeStampString(Context context, long when, boolean fullFormat)
  6. getFormatDateString(String format, long date)
  7. getLongFriendlyDate(long timeInMillis)
  8. getShortFriendlyDate(long timeInMillis)
  9. getTimeStringFrom(long timestamp)