Import Java.util.Date;
Import Java.text.DateFormat;
/**
* Format Time Class
* Dateformat.full = 0
* Dateformat.default = 2
* Dateformat.long = 1
* Dateformat.medium = 2
* Dateformat.short = 3
* @author Michael
* @version 1.0, 2007/03/09
*/
public class test{
public static void Main (String []args) {
Date d = new Date ();
String s;
/** format of the date class: Sat APR 13:17:29 CST 2006 */
System.out.println (d);
System.out.println ("******************************************");
/** getdateinstance () */
/** output Format: 2006-4-16 */
s = dateformat.getdateinstance (). Format (d);
System.out.println (s);
/** output Format: 2006-4-16 */
s = dateformat.getdateinstance (dateformat.default). Format (d);
System.out.println (s);
/** output format: Saturday, April 16, 2006 */
s = dateformat.getdateinstance (dateformat.full). Format (d);
System.out.println (s);
/** output Format: 2006-4-16 */
s = dateformat.getdateinstance (dateformat.medium). Format (d);
System.out.println (s);
/** output Format: 06-4-16 */
s = dateformat.getdateinstance (dateformat.short). Format (d);
System.out.println (s);
/** output format: 2006-01-01-00:00:00 */
Java.text.DateFormat format1 = new Java.text.SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss");
s = Format1.format (new Date ());
System.out.println (s);
/** output format: 2006-01-01-01:00:00 */
System.out.println ((New Java.text.SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss")). Format (new Date ()));
/** output format: 2006-01-01 13:00:00 */
System.out.println ((New Java.text.SimpleDateFormat (" YYYY-MM-DD HH:mm:ss ")). Format (new Date ()));
/** output format: 20060101000000***/
Java.text.DateFormat format2 = new Java.text.SimpleDateFormat ("Yyyymmddhhmmss");
s = Format2.format (new Date ());
System.out.println (s);
}
}
Add:
Calendar cal = Calendar.getinstance ();
Cal.settime (New Date ());
System.out.println ("System Date:" + cal.get (calendar.month+1));
Note that the month is starting from 0, to be calendar.month+1.
Java Date format time formatting