Java Date format time formatting

Source: Internet
Author: User
Tags dateformat

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.