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 w w w  .  j a  va2 s .  c o  m
    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