getting the end of the month for a given date
Code :
Calendar eCal = Calendar.getInstance();
eCal.set(2011,11,3);
//eCal.set(Integer.parseInt(year.getText()), Integer.parseInt(month.getSelectedItem().toString()), Integer.parseInt(day.getSelectedItem().toString()));
int iEday = eCal.getActualMaximum(eCal.DAY_OF_MONTH);
System.out.println("Month:"+ eCal.get(eCal.MONTH));
System.out.println("Max date = "+iEday);
as stated in the subject... my current codes seems to get the wrong date... when nov is being set to the Calendar, it retrun the Date as 31 while Oct will return the Date 30...
Re: getting the end of the month for a given date
Try
eCal.getActualMaximum(Calendar.DAY_OF_MONTH);
That should return 30 though as November, the 11th month, has a maximum of 30 days.
Re: getting the end of the month for a given date
no diff... still the same...
Re: getting the end of the month for a given date
Java uses 0 for January and 11 for December. I haven't looked at your code but is this your problem? To avoid problems like this you should use constants in the Calendar class.
Re: getting the end of the month for a given date
Quote:
Originally Posted by
Junky
Java uses 0 for January and 11 for December. I haven't looked at your code but is this your problem? To avoid problems like this you should use constants in the Calendar class.
oh ic... but the month is entered by the user... would my codes work if i did (Calendar.DAY_OF_MONTH+1)?
Re: getting the end of the month for a given date
How is the user inputting the month? As an int (11 for November)? Then you would use the set method and subtract one from the input.
Of course you would add some error checking on the user input.
If user input is a String ("November" or some variant) then you would need some way to parse the String into its int equivalent.
Code java:
DateFormatSymbols dfs = new DateFormatSymbols();
String[] months = dfs.getMonths();
for(int index = 0; index < months; index++) {
if(months[index].equals(userInput)) {
return index;
}
}
return -1; // indicates invalid month entered