Java gets the number of days in a month
- Calendar cal = new GregorianCalendar ();
- //or with Calendar cal = Calendar.getinstance ();
- /** Setting date**/
- SimpleDateFormat osdf = new SimpleDateFormat ("", locale.english);
- Osdf.applypattern ("yyyymm");
- try {
- System.out.println (Osdf.parse (date));
- Cal.settime (Osdf.parse (date));
- } catch (ParseException e) {
- E.printstacktrace ();
- }
- /** or set the month, note that the month is counted from 0, so use the actual month-1 is the month you want **/
- //January: Cal.set (2009, 1-1, 1);
- /** If you want to get the **/last month
- //cal.set (Calendar.day_of_month, 1);
- //date minus one to get the last day of the month time object
- //cal.add (Calendar.day_of_month,-1);
- //Output last day of the month date
- //system.out.println (Cal.get (calendar.day_of_month));
- /** starts using this method to get the maximum number of days of the month, always getting is 31 days **/
- //int num = cal.getmaximum (calendar.day_of_month);
- /** start with this method to get the maximum number of days in the actual month **/
- int num2 = Cal.getactualmaximum (calendar.day_of_month);
- System.out.println (NUM2);
Java gets the number of days in a month