public void buildArrayForMonth(int month, int year, int numOfDays, JSONArray array){
JSONObject[][] monthArray = null;
SimpleDateFormat monthFormat = new SimpleDateFormat("M");
SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
SimpleDateFormat dateFormat = new SimpleDateFormat("d");
for(int i=0;i<array.length();i++){
try {
JSONObject event = array.getJSONObject(i);
String date_full = event.getString("date_full");
Date date = new Date(HttpDateParser.parse(date_full));
int theMonth = Integer.parseInt(monthFormat.format(date)) - 1;
int theYear = Integer.parseInt(yearFormat.format(date));
int theDate = Integer.parseInt(dateFormat.format(date));
if(theMonth == month && theYear == year){
System.out.println("This Month" + date_full);
monthArray[theDate][monthArray[theDate].length] = event; //add event object to the month array and its respective date
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I essentially want thedate to be an array containing JSONObjects. My app crashes with what I have now. I’m not sure if you’re able to do this. Does Java have anything like push or add?
You forgot to create an array before initialize (I think you are getting NullPointerException in your sample code):
Also, may be HashMap will be more useful for that task.
UPD Ops, and one question, why do you need two dimensional array? I think one dimension is enough.
UPD2 And I recommend to use Calendar instead of Date and SimpleDateFormat. It is more correct way, for example:
UPD3
Update after comments. If more than one event can occurs in one day then you have use HashMap with List as I proposed.