package com.tqmall.search.common.utils;

import org.apache.commons.lang3.time.FastDateFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
 * Created by xing on 16/1/5.
 * {@link java.util.Date} 的{@link ComparableStrValueConvert}实现
 */
public class DateStrValueConvert implements ComparableStrValueConvert<Date> {

    private static final Logger log = LoggerFactory.getLogger(DateStrValueConvert.class);

    public static final DateStrValueConvert INSTANCE = new DateStrValueConvert();

    public static Date dateConvert(String input) {
        return INSTANCE.convert(input);
    }

    public static String dateFormat(Date date) {
        return INSTANCE.format(date);
    }

    private final FastDateFormat dateFormat;

    public DateStrValueConvert() {
        this("yyyy-MM-dd HH:mm:ss");
    }

    public DateStrValueConvert(String pattern) {
        dateFormat = FastDateFormat.getInstance(pattern);
    }

    @Override
    public int compare(Date o1, Date o2) {
        return o1.compareTo(o2);
    }

    @Override
    public Date convert(String input) {
        try {
            return dateFormat.parse(input);
        } catch (ParseException e) {
            log.error("解析时间, 入参input: " + input + "不符合Pattern: " + dateFormat.getPattern()
                    + ", message: " + e.getMessage());
            return null;
        }
    }

    public String format(Date date) {
        return dateFormat.format(date);
    }

    public FastDateFormat getDateFormat() {
        return dateFormat;
    }

}

@@ -34,16 +34,61 @@
 utils;%0A%0A
+import org.apache.commons.lang3.StringUtils;%0A
 import o
@@ -132,16 +132,16 @@
 Format;%0A
-
 import o
@@ -789,24 +789,126 @@
 te);%0A    %7D%0A%0A
+    public static long dateTimestamp(String input) %7B%0A        return INSTANCE.timestamp(input);%0A    %7D%0A%0A
     private 
@@ -1081,21 +1081,13 @@
     
-dateFormat = 
+this(
 Fast
@@ -1117,16 +1117,122 @@
 pattern)
+);%0A    %7D%0A%0A    public DateStrValueConvert(FastDateFormat dateFormat) %7B%0A        this.dateFormat = dateFormat
 ;%0A    %7D%0A
@@ -1375,32 +1375,85 @@
 String input) %7B%0A
+        if (StringUtils.isEmpty(input)) return null;%0A
         try %7B%0A  
@@ -1749,32 +1749,32 @@
 at(Date date) %7B%0A
-
         return d
@@ -1796,32 +1796,168 @@
 t(date);%0A    %7D%0A%0A
+    public long timestamp(String input) %7B%0A        Date date = convert(input);%0A        return date == null ? 0l : date.getTime();%0A    %7D%0A%0A
     public FastD

add DateStrValueConvert