Monday 23 July 2012

Create a Date object using the Calendar class

This example shows how to get Date object using getTime method of Java Calendar class.


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateObjectFromCalendar {

 public static void main(String[] args) {

  Calendar cal = Calendar.getInstance();

  // Create Date object using Calendar object.
  Date date = cal.getTime();

  SimpleDateFormat fmt = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");

  System.out.println("The date is:" + fmt.format(date));

 }

}


The output is:

The date is:23/07/2012 05:23:52

No comments:

Post a Comment