SimpleDateFormat类的使用

package cn.dali.code26;
/*DateFormat类:
*   功能:将系统默认的时间格式转化为自定义的格式,或者将自定义的格式转化为系统格式
*   注意:
*           这是一个抽象类,所以我们使用他的时候要创建他子类的对象。SimpleDateFormat类
*
*   SimpleDateFormat类:
*   构造方法:SimpleDateFormat(String pattern)
*       括号内为我们自定义的时间格式。
*   成员方法:
*   1.public final String format(Date date)
    {
        return format(date, new StringBuffer(),
                      DontCareFieldPosition.INSTANCE).toString();
    }
    把我们传进去的date格式时间转化为我们自定义的格式
    2.public Date parse(String source) throws ParseException
    {
        ParsePosition pos = new ParsePosition(0);
        Date result = parse(source, pos);
        if (pos.index == 0)
            throw new ParseException("Unparseable date: \"" + source + "\"" ,
                pos.errorIndex);
        return result;
    }
    把自定义格式的字符串转为date格式

    自定义格式:(区分大小写)
            y 年
            M 月
            d 日
            H 时
            m 分
            s 秒
     例:"yyyy-mm-dd HH:mm:ss"

    */

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

public class Demo02 {
    public static void main(String[] args) throws ParseException {
        SimpleDateFormat a = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
        Date date = new Date(1000L);
        System.out.println(a.format(date));
        System.out.println(a.parse("1970-00-01 08:00:01"));

    }

}

猜你喜欢

转载自blog.csdn.net/chen404897439/article/details/92560038
今日推荐