0

I have a spring boot back end and an android app.

I need to create a task based on repeat-ability, such that if I select first Monday or second Wednesday until a repetition end date , tasks should be created accordingly.

Eg: If I select first monday, start date is today i.e 31 Jan and the repetition end date is 23 March , there should be two tasks created for first monday of Feb and March respectively.

Any ideas on how I can implement this and what will the parameters be from the android app request.

Following is my code to create a task.

public void saveTask(Task task) {

        RepeatInterval repeatInterval = repeatIntervalRepository.findOne(task.getRepeatInterval().getId()); 
        task.setDateCreated(new Date());
        Date iterationDueDate = task.getRepetitionEndDate();
        Date endDate = task.getEndDate();
        if (repeatInterval.getRepeatInterval().equals("none")) {
            task.setDateCreated(new Date());
            taskRepository.save(task);

        }
        else {
            while (iterationDueDate.before(endDate)) {

                Task newTask = new Task();
                newTask.setPriority(task.getPriority());
                newTask.setTaskState(task.getTaskState());
                newTask.setDescription(task.getDescription());
                newTask.setSystemUser(task.getSystemUser());
                newTask.setName(task.getName());
                newTask.setEndDate(task.getEndDate());
                newTask.setChecklist(task.getChecklist());
                newTask.setRepetitionEndDate(task.getRepetitionEndDate());
                newTask.setDateCreated(new Date());
                newTask.setProject(task.getProject());
                newTask.setRepeatInterval(task.getRepeatInterval());
                taskRepository.save(newTask);
                newTask.setRepetitionEndDate(updateDueDate(iterationDueDate, repeatInterval.getRepeatInterval()));
                iterationDueDate = newTask.getRepetitionEndDate();
            }
        }
    }

    private Date updateDueDate(Date previousDate, String repeatInterval) {

        Date newDate = null;
        Calendar cal = Calendar.getInstance();
        cal.setTime(previousDate);

        if (repeatInterval.equalsIgnoreCase("hourly")) {
            cal.add(Calendar.HOUR, 1);
        } else if (repeatInterval.equalsIgnoreCase("daily")) {
            cal.add(Calendar.DAY_OF_MONTH, 1);
        } else if (repeatInterval.equalsIgnoreCase("weekly")) {
            cal.add(Calendar.DAY_OF_MONTH, 7);
        } else if (repeatInterval.equalsIgnoreCase("monthly")) {
            cal.add(Calendar.MONTH, 1);
        } else if (repeatInterval.equalsIgnoreCase("yearly")) {
            cal.add(Calendar.YEAR, 1);
        }
        return cal.getTime();
    }

Any help will be appreciated.

A.S
  • 798
  • 1
  • 10
  • 32

1 Answers1

0

You could use Joda-Time which was added as the new datetime library in Java 8

https://github.com/dlew/joda-time-android

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Daniel Puiu
  • 962
  • 6
  • 21
  • 29
  • Joda-Time is now in maintenance. The team advises moving to java.time classes. Joda-Time inspired java.time but is not the same, entirely re-engineered. – Basil Bourque Jan 31 '17 at 21:02