JAVA一周学习笔记之DateFormat

java.text.DateFormat:是日期/时间格式化子类的抽象类
作用:
格式化(也就是日期-->文本),解析(文本-->日期)
成员方法:
String format(Date date)按照指定的模式,把Date日期,格式化为符合模式的字符串
Date parse(String source)把符合模式的字符串,解析为Date日期
DateFormat类是一个抽象类,无法直接创建对象使用,可以使用DateFormat的子类

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/*
  java.text.DateFormat:是日期/时间格式化子类的抽象类
  作用:
    格式化(也就是日期-->文本),解析(文本-->日期)
  成员方法:
    String format(Date date)按照指定的模式,把Date日期,格式化为符合模式的字符串
    Date parse(String source)把符合模式的字符串,解析为Date日期
    DateFormat类是一个抽象类,无法直接创建对象使用,可以使用DateFormat的子类
 */
public class demo01DateFormat {
    public static void main(String[] args) throws ParseException {
//        demo01();
        demo02();
    }
    /*
      使用DateFormat类中的方法parse,把文本解析为日期
      Date parse(String source)把符合模式的字符串,解析为Date日期
      使用步骤:
          1.创建SimpleDateFormat对象,构造方法中传递指定模式
          2.调用SimpleDateFormat对象中的方法parse,把符合构造方法中模式的字符串,解析为Date日期

       */
    private static void demo02() throws ParseException {
        //1.创建SimpleDateFormat对象,构造方法中传递指定的模式
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
        //2.调用SimpleDateFormat对象中的方法parse,把符合构造方法中模式的字符串,解析为Date日期
        Date date = sdf.parse("2088年08月08日 17时01分10");
        System.out.println(date);
    }


    /*
    使用DateFormat类中的方法format,把日期格式化为文本
    String format(Date date) 按照指定的模式,把Date日期,格式化为符合模式的字符串
    使用步骤:
        1.创建SimpleDateFormat对象,构造方法中传递指定的模式
        2.调用SimpleDateFormat对象中的方法format,按照构造方法中指定的模式,把Date日期格式化为符合模式的字符串(文本)

     */
    private static void demo01(){
        //1.创建SimpleDateFormat对象,构造方法中传递指定的模式
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=new Date();
        String text=sdf.format(date);
        System.out.println(date);//Sun Jul 19 19:59:48 GMT+08:00 2020
        System.out.println(text);//2020-07-19 19:59:48
    }
}

 

posted @ 2020-07-28 14:54  JasonLin233  阅读(208)  评论(0)    收藏  举报