Java Data Type How to - Get day value in a month from Date








Question

We would like to know how to get day value in a month from Date.

Answer

/* w w  w .  j  a  v  a 2s .c o m*/
import java.util.Calendar;
import java.util.Date;

public class Main {
  public static void main(String[] argv) {
    System.out.println(getDay(new Date()));
  }

  public static Integer getDay(Date date) {
    if (date == null)
      return 0;
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    return cal.get(Calendar.DAY_OF_MONTH);
  }
}

The code above generates the following result.