Tommy Chong
Tommy Chong

Reputation: 386

Get dates array within a date range

How to store a date into an array from date between date by loop?

Example 1:
First Date = 26/9/2018
Last Date = 30/9/2018

I want to store the date by loop like this below:

new Date["26/9/2018","27/9/2018","28/9/2018","29/9/2018","30/9/2018"]

Something like:

for(int i = 0; FirstDate < LastDate; i++){
   //add array here by loop
}

So I got new Date[4] when I select as below:

new Date[3], it will show 29/9/2018

Upvotes: 1

Views: 925

Answers (3)

Mohammad Samdi
Mohammad Samdi

Reputation: 111

Simply use this code:

public Date[] getDateLoop() {
  String str_start_date = "26/9/2018";
  String str_end_date = "30/9/2018";
  SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
  //Date date = sdf.parse(dateStr);
  Date start_date = null;
  try {
      start_date = sdf.parse(str_start_date);
  } catch (ParseException e) {
      e.printStackTrace();
  }

  Date end_date = null;
  try {
      end_date = sdf.parse(str_end_date);
  } catch (ParseException e) {
      e.printStackTrace();
  }

  long diff = TimeUnit.DAYS.convert(end_date.getTime() - start_date.getTime(), TimeUnit.MILLISECONDS);

  Date[] array_date = new Date[(int) diff + 1];
  for (int i = 0; i <= diff; i++) {
      Date temp_date = start_date;
      Calendar c = Calendar.getInstance();
      c.setTime(temp_date);
      c.add(Calendar.DATE, i);
      temp_date = c.getTime();
      array_date[i] = temp_date;
  }
  return array_date;
}

Upvotes: 0

navylover
navylover

Reputation: 13579

You could try this:

        List<Date> dates = new ArrayList<Date>();

        String start_date ="26/9/2018";
        String end_date ="30/9/2018";

        DateFormat formatter ;

        formatter = new SimpleDateFormat("dd/M/yyyy");
        Date  startDate = null;
        Date  endDate = null;
        try {
            startDate = formatter.parse(start_date);
            endDate = formatter.parse(end_date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        long interval = 24*1000 * 60 * 60;
        long endTime =endDate.getTime() ;
        long curTime = startDate.getTime();
        while (curTime <= endTime) {
            dates.add(new Date(curTime));
            curTime += interval;
        }
        for(int i=0;i<dates.size();i++){
            String result = formatter.format(dates.get(i));
        }

May be not optimum, but tested successfully, hope helpful.

Upvotes: 0

Nicola Gallazzi
Nicola Gallazzi

Reputation: 8713

I suggest you to use LocalDate api by Jake Warthon. Date api is not very reliable in android. By the way, try this solution:

public ArrayList<LocalDate> getDateLoop() {
        LocalDate startDate = LocalDate.of(2018, 9, 26);
        LocalDate endDate = LocalDate.of(2018, 9, 30);
        ArrayList<LocalDate> dates = new ArrayList<>();

        for (int i = startDate.getDayOfMonth(); i <= endDate.getDayOfMonth(); i++) {
            LocalDate dateToAdd = LocalDate.of(startDate.getYear(), startDate.getMonth(), i);
            dates.add(dateToAdd);
        }
        return dates;
    }

Here it is library's link: https://github.com/JakeWharton/ThreeTenABP

Upvotes: 1

Related Questions