Home > Java > javaTutorial > body text

Sample code sharing for Java date format time formatting operation

黄舟
Release: 2017-03-27 10:42:21
Original
1083 people have browsed it

This article mainly introduces the Java date format time formatting operation, and analyzes the related implementation techniques of Java for date and time formatting operations based on specific examples. Friends in need can refer to the examples of this article

Describes Java date format time formatting operations. Share it with everyone for your reference, the details are as follows:

import java.util.Date;
import java.text.DateFormat;
/**
* 格式化时间类
* 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类的格式: Sat Apr 16 13:17:29 CST 2016 */
    System.out.println(d);
    System.out.println("******************************************");
    /** getDateInstance() */
    /** 输出格式: 2016-4-16 */
    s = DateFormat.getDateInstance().format(d);
    System.out.println(s);
    /** 输出格式: 2016-4-16 */
    s = DateFormat.getDateInstance(DateFormat.DEFAULT).format(d);
    System.out.println(s);
    /** 输出格式: 2016年4月16日 星期六 */
    s = DateFormat.getDateInstance(DateFormat.FULL).format(d);
    System.out.println(s);
    /** 输出格式: 2016-4-16 */
    s = DateFormat.getDateInstance(DateFormat.MEDIUM).format(d);
    System.out.println(s);
    /** 输出格式: 06-4-16 */
    s = DateFormat.getDateInstance(DateFormat.sHORT).format(d);
    System.out.println(s);
    /** 输出格式: 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);
    /** 输出格式: 2016-01-01 01:00:00 */
    System.out.println((new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss")).format(new Date()));
    /** 输出格式: 2016-01-01 13:00:00 */
    System.out.println((new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date()));
    /** 输出格式: 20160101000000***/
    java.text.DateFormat format2 = new java.text.SimpleDateFormat("yyyyMMddhhmmss");
    s = format2.format(new Date());
    System.out.println(s);
  }
}
Copy after login

Add:

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

The above is the detailed content of Sample code sharing for Java date format time formatting operation. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!