Java date format time formatting operation example

  • 2020-06-15 09:02:22
  • OfStack

This article is an example of Java date format time formatting. To share for your reference, specific as follows:


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
*/
public class Test{
  public static void main(String []args){
    Date d = new Date();
    String s;
    /** Date The format of the class : Sat Apr 16 13:17:29 CST 2016 */
    System.out.println(d);
    System.out.println("******************************************");
    /** getDateInstance() */
    /**  The output format : 2016-4-16 */
    s = DateFormat.getDateInstance().format(d);
    System.out.println(s);
    /**  The output format : 2016-4-16 */
    s = DateFormat.getDateInstance(DateFormat.DEFAULT).format(d);
    System.out.println(s);
    /**  The output format : 2016 years 4 month 16 day   week 6 */
    s = DateFormat.getDateInstance(DateFormat.FULL).format(d);
    System.out.println(s);
    /**  The output format : 2016-4-16 */
    s = DateFormat.getDateInstance(DateFormat.MEDIUM).format(d);
    System.out.println(s);
    /**  The output format : 06-4-16 */
    s = DateFormat.getDateInstance(DateFormat.sHORT).format(d);
    System.out.println(s);
    /**  The output format : 2016-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);
    /**  The output format : 2016-01-01 01:00:00 */
    System.out.println((new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss")).format(new Date()));
    /**  The output format : 2016-01-01 13:00:00 */
    System.out.println((new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date()));
    /**  The output format : 20160101000000***/
    java.text.DateFormat format2 = new java.text.SimpleDateFormat("yyyyMMddhhmmss");
    s = format2.format(new Date());
    System.out.println(s);
  }
}

Supplement 1:


Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
System.out.println("System Date: " + cal.get(Calendar.MONTH+1));

Note that the month starts at 0 and requires Calendar. MONTH+1.

PS: Here are some more time and date related tools for your reference:

Online Date/Day calculator:
http://tools.ofstack.com/jisuanqi/date_jisuanqi

Online date calculator/Difference day calculator:
http://tools.ofstack.com/jisuanqi/datecalc

Online date difference calculator:
http://tools.ofstack.com/jisuanqi/onlinedatejsq

Unix timestamp (timestamp) conversion tool:
http://tools.ofstack.com/code/unixtime

More java related content interested readers can see the special topics: java date and time operation skills summary, Java picture operation skills Summary, Java Operation DOM node skills Summary, Java File and directory operation skills Summary and Java Data Structure and Algorithm Tutorial.

I hope this article has been helpful in java programming.


Related articles: