Create java.sql.Date Using java.util.Calendar in Java

Description

The following code shows how to create java.sql.Date Using java.util.Calendar.

Example


  /*ww  w  .j  a va  2  s  .  c  om*/

import java.util.Calendar;

public class Main {

  public static void main(String[] args) {

    // get a calendar using the default time zone and locale.
    Calendar calendar = Calendar.getInstance();

    // set Date portion to January 1, 1970
    calendar.set(Calendar.YEAR, 1970);
    calendar.set(Calendar.MONTH, Calendar.JANUARY);
    calendar.set(Calendar.DATE, 1);

    // normalize the object
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);

    java.sql.Date javaSqlDate = new java.sql.Date(calendar.getTime().getTime());
  }

}




















Home »
  Java Tutorial »
    JDBC »




Batch
Binary Data
Database
Date Time
Insert
ResultSet
SQL
Statement
Stored Function
Table