Create variable names using a loop in Java?

Posted by SeerUK on Stack Overflow See other posts from Stack Overflow or by SeerUK
Published on 2010-04-19T06:55:18Z Indexed on 2010/04/19 7:13 UTC
Read the original article Hit count: 417

Filed under:
|
|
|
|

Hi, first time poster, long time reader so be gentle with me :)

See the following code which works to generate me timestamps for the beginning and end of every month in a financial year.

int year = 2010;
// Financial year runs from Sept-Aug so earlyMonths are those where year = FY-1 and lateMonths are those where year = FY
int[] earlyMonths = {8, 9, 10, 11}; // Sept to Dec
int earlyYear = year -1;
for (int i : earlyMonths) {
    month = i;
    Calendar cal = Calendar.getInstance();
    cal.clear();
    cal.set(earlyYear,month,1,0,0,0);
    Long start = cal.getTimeInMillis();
    cal.clear();
    cal.set(earlyYear,month,1);
    lastDayofMonth = cal.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
    cal.set(earlyYear,month,lastDayofMonth,23,59,59);
    Long end = cal.getTimeInMillis();
}
int[] lateMonths = {0, 1, 2, 3, 4, 5, 6, 7}; // Jan to Aug
for (int i : lateMonths) {
    month = i;
    Calendar cal = Calendar.getInstance();
    cal.clear();
    cal.set(year,month,1,0,0,0);
    Long start = cal.getTimeInMillis();
    cal.clear();
    cal.set(year,month,1);
    lastDayofMonth = cal.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
    cal.set(year,month,lastDayofMonth,23,59,59);
    Long end = cal.getTimeInMillis();
}

So far so good, but in order to use these results I need these timestamps to be output to variables named by month (to be used in a prepared statement later in the code. e.g. SeptStart = sometimestamp, SeptEnd = some timestamp etc etc.

I don't know if it is possible to declare new variables based on the results of each loop. Any ideas?

© Stack Overflow or respective owner

Related posts about java

Related posts about variables