Java - Write code to set Year Month Day to create a Calendar

Requirements

Write code to set Year Month Day to create a Calendar

Demo

import java.util.Calendar;
import java.util.Date;

public class Main {
  public static void main(String[] argv) {
    int year = 1942;
    int month = 2;
    int day = 4;/*from www  . j  a  v a  2s  . c om*/
    System.out.println(setYearMonthDay(year, month, day));
  }

  public static Date setYearMonthDay(int year, int month, int day) {
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, year);
    cal.set(Calendar.MONTH, month - 1);
    cal.set(Calendar.DAY_OF_MONTH, day);
    return cal.getTime();
  }
}

Related Exercise