DateParse工具类

======================日期解析工具类

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

public class DateParseUtil {
    
    public static final String DATE_PATTERN_yyyyMMddHHmmss = "yyyyMMddHHmmss";
    public static final String DATE_PATTERN_yyyyMMdd_HHmmss = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_PATTERN_yyyyMMdd = "yyyyMMdd";
    public static final String DATE_PATTERN_yyyy_MM_dd = "yyyy-MM-dd";
    
    public static Date parseDate(String inputDateStr, String pattern){
        SimpleDateFormat formatUtil = new SimpleDateFormat(pattern);
        Date resDate = null;
        try {
            resDate = formatUtil.parse(inputDateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        } 
        return resDate;
    }
}

======================日期解析测试类

public class DateParseUtilTest extends _BaseTest{
    
    /**
     * 解析输入日期字符串
     */
    @Test
    public void test_parseDate() {
//      String inputDateStr = "20211221";
//      Date parseInputDate = DateParseUtil.parseDate(inputDateStr, DateParseUtil.DATE_PATTERN_yyyyMMdd);
        
//      String inputDateStr = "2021-12-21";
//      Date parseInputDate = DateParseUtil.parseDate(inputDateStr, DateParseUtil.DATE_PATTERN_yyyy_MM_dd);
        
        String inputDateStr = "2021-12-21 22:38:01";
        Date parseInputDate = DateParseUtil.parseDate(inputDateStr, DateParseUtil.DATE_PATTERN_yyyyMMdd_HHmmss);
        
        //输出解析后的日期,作为比对
        String formatDate = DateFormatUtil.formatDate(parseInputDate, DateFormatUtil.DATE_PATTERN_yyyyMMdd_HHmmss);
        System.out.println(formatDate); //2021-12-21 00:00:00 || 2021-12-21 22:38:01
    }
}

 

上一篇:设计模式-概括


下一篇:TOMcat在MYSQL的显示