Get last Day of a month - Android java.util

Android examples for java.util:Day

Description

Get last Day of a month

Demo Code

import android.annotation.SuppressLint;
import android.text.format.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class Main{

  public static final String YEAR = "yyyy";
  public static final String MONTH = "MM";
  public static final String DAY = "dd";
  public static final String FULL_TIME = "HH:mm:ss";
  public static final String YEAR_MONTH = YEAR + "-" + MONTH;
  public static final String FULL_DATE = YEAR_MONTH + "-" + DAY;
  public static final String FULL_DATE_TIME = FULL_DATE + " " + FULL_TIME;
  public static final String FULL_TIME_ZONE = FULL_DATE + "'T'"
          + FULL_TIME + "'Z'";

  /**//from   w  w  w . j a  v a2  s .  com
   * <br>
   * Convert date format
   * @param format
   * @param date
   * @return String
   */
  private static String format(String format, Date date) {
      SimpleDateFormat f = new SimpleDateFormat(format);
      return f.format(date);
  }
    public static int lastDay(int $year, int $month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, $year);
        calendar.set(Calendar.MONTH, $month);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH,
                calendar.get(Calendar.DAY_OF_MONTH) - 1);
        return Integer.parseInt(format(DAY, calendar.getTime()));
    }

}

Related Tutorials