Thursday, July 10, 2008

java.util.Calendar package

/*This class shows the different different formta of date.
*dd-mm-yyyy
*dd-MM-yyyy-mm-ss
*The java.util.Calendar class is used to represent the date and time.
The year, month, day, hour, minute, second, and milliseconds can all
be set or obtained from a Calendar object. The default Calendar object
has the current time in it. There are also methods for making data calculations.
*/
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DataFormats
{

public static final String DATE_FORMAT_1 = "yyyy-MM-dd";
public static final String DATE_FORMAT_2 = "yyyy-MM-dd-mm-ss";
public static void main(String[] args)
{
String ddMMYyyy=getDDMMYYYY();
String ddMMYyyymmss=getDDMMYYYYmmss();
System.out.println("Date in dd-MM-yyyy Format = "+getDDMMYYYY());
System.out.println("Date in dd-MM-yyyy-mm-ss Format = "+getDDMMYYYYmmss());
}
//Method for date in dd-MM-yyyy format
public static String getDDMMYYYY()
{
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_1);
return sdf.format(cal.getTime());
}
//Method for date in dd-MM-yyyy-mm-ss format
public static String getDDMMYYYYmmss()
{
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_2);
return sdf.format(cal.getTime());
}

}

0 comments: