工具类(一)-日期格式化工具类

工具类(一)-日期格式化工具类

package com.wang.mygateway.gateway.basecore.utils;

import org.apache.dubbo.common.utils.StringUtils;

import javax.xml.crypto.Data;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {

    private static final String format = "yyyy-MM-dd HH-mm-ss";

    private DateUtil(){}

    public static String format(Date time){
        return new SimpleDateFormat(format).format(time);
    }

    public String format(Date time,String format){
        if (StringUtils.isBlank(format)){
            return format(time);
        }else{
            return new SimpleDateFormat(format).format(time);
        }
    }

    public static String format(Long time){
        return new SimpleDateFormat(format).format(new Date(time));
    }

    public static String format(Long time,String format){
        if (StringUtils.isBlank(format)){
            return format(time);
        }else{
            return new SimpleDateFormat(format).format(new Date(time));
        }
    }

    public static Date parse(String time){
        try {
            return new SimpleDateFormat(format).parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Date parse(String time,String format){
        if (StringUtils.isBlank(format)){
            return parse(time);
        }else {
            try {
                return new SimpleDateFormat(format).parse(time);
            } catch (ParseException e) {
                e.printStackTrace();
                return null;
            }
        }
    }
}
上一篇:你来到这个世界上已经几天了-例子


下一篇:Java Date日期和String字符串互转