Saturday, February 21, 2009

Creating a Date object in java

Creating instances of java.util.Date & java.sql.Date classes
using a Calendar Class in java.


Java Code:

import java.util.Calendar;

public class DateUtil
{
public void createDates()
{
int year = 2006;
int month = 0; //January
int date = 1;
Calendar cal = Calendar.getInstance();
//Clear all fields
cal.clear();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DATE, date);

//Create instance of java.util.Date
java.util.Date utilDate = cal.getTime();

//Create instance of java.sql.Date
java.sql.Date sqlDate = new java.sql.Date(cal.getTimeInMillis());

System.out.println(utilDate);
System.out.println(sqlDate);
}

public static void main(String[] args) {
DateUtil dateutil = new DateUtil();
dateutil.createDates();
}
}

0 comments:

Blog Widget by LinkWithin

JS-Kit Comments

  © Blogger template Newspaper III by Ourblogtemplates.com 2008

Back to TOP