1

DateFormat class


java.text.DateFormat is a class that converts Date (date) to Strting (text) ( abstract class ).

format: Date is converted to String object.

Analysis of String is converted to Date object.


The commonly used methods are

  • format method
  • parse method

Abstract classes cannot directly call subclasses when needed:

Subclass: java.text.SimpleDateFormat This class needs a format to define the usage inside.

y = year

M = month

d = day

H = hour

m = minutes

s = seconds

Teach you notation (small and small) to write.


Common methods in java.text.SimpleDateFormat

the format Date object as a string format.
parse disassembles the string into Date objects.


####  format方法
     Date date = new Date();
     //创建时间格式 
     SimpleDateFormat simpleDateFormat  = new SimpleDateFormat("yyyy年MM月dd日 HH小时mm分ss秒");
    // String 转为date
     String str = simpleDateFormat.format(date);
    //打印
     System.out.println(str);

output : your year + month + hour + minute + second

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Demo03 {
    public static void main(String[] args) throws ParseException {

        DateFormat df  = new SimpleDateFormat("yyyy年MM月dd日");


        String str1 = "2018年12月11日";
        Date date1 = df.parse(str1);

        System.out.println(date1);


    }
}

output result : Tue Dec 11 00:00:00 CST 2018


嘻嘻硕
27 声望12 粉丝

想当一只天然呆的鸭qwq