All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gitee.feizns.convert.DateConvertUtils Maven / Gradle / Ivy

There is a newer version: 5.5-RELEASE
Show newest version
package com.gitee.feizns.convert;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * @author feizns
 * @since 2019/5/19 0019
 */
public abstract class DateConvertUtils {

    /**
     * 毫秒转日期
     */
    private static final Map, Function>
            MILLISECOND_CONVERSION = new HashMap<>();

    /**
     * 日期格式
     */
    private static final List PATTERNS = new ArrayList<>();

    static {
        //毫秒到日期
        {
            MILLISECOND_CONVERSION.put(Date.class, Date::new);
            MILLISECOND_CONVERSION.put(java.sql.Date.class, java.sql.Date::new);
            MILLISECOND_CONVERSION.put(java.sql.Timestamp.class, java.sql.Timestamp::new);
            MILLISECOND_CONVERSION.put(java.sql.Time.class, java.sql.Time::new);
        }

        //常用格式
        {
            PATTERNS.add("yyyy-MM-dd HH:mm:ss");
            PATTERNS.add("yyyy/MM/dd HH:mm:ss");
            PATTERNS.add("yyyy-MM-dd'T'HH:mm:ss");
            PATTERNS.add("yyyy-MM-dd");
            PATTERNS.add("yyyy/MM/dd");
            PATTERNS.add("HH:mm:ss");
        }
    }

    /**
     * 字符日期转毫秒
     * @param date
     * @return
     */
    public static final Long toMillisecond(String date, String pattern) {
        try {
            return new SimpleDateFormat(pattern).parse(date).getTime();
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 常用格式转换
     * @param date
     * @return
     */
    public static final Long toMillisecond(String date) {
        for (String pattern : PATTERNS) {
            Long millisecond = toMillisecond(date, pattern);
            if ( millisecond != null )
                return millisecond;
        }
        return null;
    }

    /**
     * 毫秒转日期
     * @param milliSecond
     * @param targetType
     * @param 
     * @return
     */
    public static final  T to(long milliSecond, Class targetType) {
        Function longFun = MILLISECOND_CONVERSION.get(targetType);
        return longFun != null ? (T) longFun.apply(milliSecond) : null;
    }

    /**
     * 转日期
     * @param original
     * @param targetType
     * @param 
     * @return
     */
    public static final  T to(Object original, Class targetType) {
        if ( original != null && targetType.isAssignableFrom(original.getClass()) == false ) {
            if ( original instanceof Number ) {
                return to(((Number) original).longValue(), targetType);
            } else {
                Long millisecond = toMillisecond(original.toString());
                return millisecond != null ? to(millisecond, targetType) : null;
            }
        }
        return (T) original;
    }

    /**
     * 是否支持
     * @param targetType
     * @return
     */
    public static final boolean isSupport(Class targetType) {
        return MILLISECOND_CONVERSION.containsKey(targetType);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy