Assigning Strings to a Date List

Asked

Viewed 100 times

2

I need to assign for each day a list of dates of an interval period, a given String among three possible (Class A, Class B, Class C), so that the result is sequential until filling all the dates of the list.

Follow code done so far....

import java.util.*;
import java.text.*;

public class CalcularDatas {

    public static void main (String[] args) throws ParseException {

        String[] myArray = {"Turma A", "Turma B", "Turma C"};
        List<String> arrayList = new ArrayList<String> (Arrays.asList(myArray));

        DateFormat df = new SimpleDateFormat ("dd/MM/yyyy");
        Date dt1 = df.parse ("01/10/2016"); // Data inicial
        Date dt2 = df.parse ("10/10/2016"); // 
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime (dt1);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime (dt2);
        cal2.add(Calendar.DATE,1);
        for (Calendar cal = cal1; cal.compareTo (cal2) <= 0; cal.add (Calendar.DATE, 1)) {
            System.out.println (df.format (cal.getTime())+ arrayList);
        }
    }
}

Exit:

01/10/2016[Class A, Class B, Class C]
02/10/2016[Class A, Class B, Class C]
03/10/2016[Class A, Class B, Class C]
04/10/2016[Class A, Class B, Class C]
05/10/2016[Class A, Class B, Class C]
06/10/2016[Class A, Class B, Class C]
07/10/2016[Class A, Class B, Class C]
08/10/2016[Class A, Class B, Class C]
09/10/2016[Class A, Class B, Class C]
10/10/2016[Class A, Class B, Class C]
11/10/2016[Class A, Class B, Class C]

Expected mode:

01/10/2016 Class A
02/10/2016 Class B
03/10/2016 Class C
04/10/2016 Class A
05/10/2016 Class B
06/10/2016 Class C
07/10/2016 Class A
08/10/2016 Class B
09/10/2016 Class C
10/10/2016 Class A
11/10/2016 Class B

2 answers

3

If you order print arrayList, obviously it will print it whole. The solution is to use an iterator, increment it modularly and print only the position of that index:

int i = 0;
for (Calendar cal = cal1; cal.compareTo(cal2) <= 0; cal.add(Calendar.DATE, 1)) {
    System.out.println(df.format(cal.getTime()) + " " + arrayList.get(i));
    i = (i + 1) % 3;
}
  • 1

    Interesting solution! + 1 :)

2

One of the possible solutions is to use Iterator under his ArrayList:

public static void main (String[] args) {
    String[] myArray = {"Turma A", "Turma B", "Turma C"};
    List<String> arrayList = new ArrayList<String> (Arrays.asList(myArray));

    DateFormat df = new SimpleDateFormat ("dd/MM/yyyy");
    Date dt1 = df.parse ("01/10/2016"); // Data inicial
    Date dt2 = df.parse ("10/10/2016"); // 
    Calendar cal1 = Calendar.getInstance();
    cal1.setTime (dt1);
    Calendar cal2 = Calendar.getInstance();
    cal2.setTime (dt2);
    cal2.add(Calendar.DATE,1);
    //cria o iterator sob o arraylist
    Iterator<String> it = arrayList.iterator();

    for (Calendar cal = cal1; cal.compareTo (cal2) <= 0; cal.add (Calendar.DATE, 1)) {
        //checa se o iterator já chegou ao fimm
        //se sim, o reinicia
        if(!it.hasNext()){
            it = arrayList.iterator();      
        }
        // next() exibe o item seguinte do arraylist
        System.out.println (df.format (cal.getTime()) + it.next());
    }
}

Upshot:

01/10/2016 Class A
02/10/2016 Class B
03/10/2016 Class C
04/10/2016 Class A
05/10/2016 Class B
06/10/2016 Class C
07/10/2016 Class A
08/10/2016 Class B
09/10/2016 Class C
10/10/2016 Class A
11/10/2016 Class B

See working on IDEONE

  • Both codes worked perfectly. Thanks for the help!

  • 1

    @Mecsr you can accept one of the answers by clicking on and you can also vote on both, click the arrow up both.

  • And if I want to associate a schedule to each class. This would be the best way? It would have to join the two arrays in one?

  • @Mecsr Wants to save this output as a third Array?

  • And if I want to associate a schedule to each class. Could I join the arrays? String[] myArrayTurma = {"Class A", "Class B", "Class C"}; String[] myArrayHorario = {"08:00 at 12:00", "12:00 at 18:00", "18:00 at 24:00"}; List<String> arrayListHorario = new Arraylist<String> (Arrays.asList(myArrayHorario)); if(! iteratorHorario.hasNext(){ iteratorHorario = arrayListHorario.iterator(); } System.out.println (df.format (cal.getTime()) + " " + iteratorTurma.next() + " " + iteratorHorario.next.());

  • The idea would be this, save the exit as a third Array. However the schedules would not be fixed, it would not always be the same schedule for each Class. These schedules would be created according to which each Class was created. When I create class 1 I will inform you what the schedule of this class will be, when I create class 2 I will inform your schedule and so on.... That would be the idea.

  • @Let me give you a suggestion, instead of creating countless arrays, why don’t you create a POJO class? Thus, you concentrate all class information on one class, and save each class as if it were an object, for example: http://pastebin.com/4Cu8vVQT

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.