English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

example: i have an instance of a Date object. i need to get the first and last date of the particular month. e.g. Aug first - 1 and last - 31...

2007-08-18 04:39:01 · 2 answers · asked by nick 1 in Computers & Internet Programming & Design

2 answers

Assuming you have:

Date myDate;

You'd do this:

Calendar c = new Calendar();
c.setTime(myDate);

int lastDayOfMonth = c.getActualMaximum( Calendar.DAY_OF_MONTH);
int firstDayOfMonth = c.getActualMinimum( Calendar.DAY_OF_MONTH);

===============
Note that "getMaximum()" suggested by a respondent above, always replies the biggest number for ANY month (31). No matter what date is in the Calendar, you'd get 31 back. That's probably not what you want.

getActualMaximum() returns the biggest number possible for the requested field, given the current time value in the Calendar. So for February in a non-leap-year it would answer 28, and for February in a leap year it would answer 29, etc. I think that is what you are looking for.

You can call getActualMinimum() for completeness, but the 1st is the first day of every month in our Calendar, so you can just use "1."

2007-08-18 08:47:06 · answer #1 · answered by McFate 7 · 1 0

Calendar cal = new Calendar();
cal.getMaximum(DAY_OF_MONTH);

2007-08-18 04:52:43 · answer #2 · answered by Anonymous · 0 0

fedest.com, questions and answers