Create a java.util.Date Object from a Year, Month, Day Format in Java

Description

The following code shows how to create a java.util.Date Object from a Year, Month, Day Format.

Example


//w ww.j  a va2 s .  c o  m
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class Main {

  public static void main(String[] args) throws ParseException {
    int year = 2003;
    int month = 12;
    int day = 12;

    String date = year + "/" + month + "/" + day;
    java.util.Date utilDate = null;

    try {
      SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
      utilDate = formatter.parse(date);
      System.out.println("utilDate:" + utilDate);
    } catch (ParseException e) {
      System.out.println(e.toString());
      e.printStackTrace();
    }

  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Date »




Date Get
Date Set
Date Format
Date Compare
Date Convert
Date Calculation
Date Parse
Timezone