1

I am having trouble figuring a problem i have. I have to create a banking system which stores data of incoming and out going expenses, so far i have created an Array of objects each of which saves the Name, amount, and a number of weeks, eg 4. If i know that this expense happens every 4 weeks how can i auto generate dates starting from the 1st of this year to the end, for example, wage +4000, 1/1/12, wage +4000, 29/1/12 and so on?

How do i auto generate a list of dates if i know it should happen every X amount of weeks?

1
  • How many days are there in 4 weeks? From that, you can do a simple increment on a counter variable. Finally, you need to find the calendar date of the nth day of the year. I suggest you put away the laptop and pick up a calendar to look for ideas how to accomplish this last step. Apr 7, 2013 at 21:54

2 Answers 2

0

Here is an improvement on @Héctor van den Boorn answer.

Calendar date= Calendar.getInstance();
ArrayList<Calendar> dates = new ArrayList<>();

for (int i=0; i<10; i++)
{
  date.add(Calendar.WEEK_OF_YEAR, 4);
  // Create new instance of cal
  Calendar tmp= Calendar.getInstance();
  //Makes its inner values the same
  tmp.setTime(date.getTime());
  // Add unique instance to list
  dates.add(tmp);
}
-1

You can use the Java Calendar class.

Calendar date= Calendar.getInstance();
ArrayList<Calendar> dates = new ArrayList<>();

for (int i=0; i<10; i++)
{
  date.add(Calendar.WEEK_OF_YEAR, 4);
  dates.add(date);
}
5
  • 1
    What is now? Where is it defined?
    – PM 77-1
    Apr 7, 2013 at 22:04
  • sorry, replace now by date (I changed the variable's name) Apr 7, 2013 at 22:37
  • This code does not work. All the dates in the resulting dates ArrayList are the same. Jul 11, 2015 at 19:05
  • @stackoverflowuser2010: try creating date inside the loop. Jul 12, 2015 at 9:15
  • This code is incorrect as the same instance of the calendar will be added to the list each time. resulting in the same calendar instance being in the list multiple times. Apr 14, 2016 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.