1. 程式人生 > >時間序列化--SimpleDateFormat

時間序列化--SimpleDateFormat

時間序列化
* 字串--date--字串
* 2018-09-12 11:28:30     ====>20180912
package com.carlinfo.bigdata;

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

/**
 * 時間序列化
 * 字串--date--字串
 * 2018-09-12 11:28:30     ====>20180912
 */
public class Ops2
{
    public static void main(String[] args) throws ParseException
    {
        /**
         * 先new一個SimpleDateFormat,規定date格式
         */
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        /**
         * 對new出來的SimpleDateFormat進行parse解析,把要解析的字串放進去
         */
        Date date = format1.parse("2018-09-12 11:28:30");
        /**
         * 再new一個SimpleDateFormat,用來規定最後輸出的字串
         */
        SimpleDateFormat format2 = new SimpleDateFormat("yyyyMMdd");
        /**
         * 用format2的格式要求處理date
         */
        String res = format2.format(date);
        /**
         * 輸出res
         */
        System.out.println(res);
    }
}