Java Month create from Date

Description

Java Month create from Date

import java.time.Month;
import java.util.Calendar;
import java.util.Date;

public class Main {

  public static void main(String[] args) {
    System.out.println(getMonth(new Date()));
  }/*from   w  w  w  .j  a  v a 2  s.c  o m*/

  public static Month getMonth(Date date) {
    return Month.of(getMonthInt(date));
  }

  public static int getMonthInt(Calendar calendar) {
    return calendar.get(Calendar.MONTH) + 1;
  }

  public static int getMonthInt(Date date) {
    return getMonthInt(getCalendar(date));
  }

  public static Calendar getCalendar(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);

    return cal;
  }
}



PreviousNext

Related