铁马冰河2000

导航

日期解析工具DateParseUtil

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

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 static Date parseDate(long DateLong){
        return new Date(DateLong);
    }
}

 

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

    /**
     * 解析输入日期字符串
     */
    @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);
        
        long inputDateLong = 1420041600000L;
        Date parseInputDate = DateParseUtil.parseDate(inputDateLong);
        
        //输出解析后的日期,作为比对
        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
    }

 

posted on 2021-12-21 22:49  铁马冰河2000  阅读(187)  评论(1编辑  收藏  举报