Java 从String类型数据获取时间

package com.saiita.service;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DateFormatUtil {
    /**
     *
     * @Title: longToString
     * @Description:把13位的毫秒值转化为String类型格式化的时间格式
     * @param time
     * @return:
     * @throws
     */
    public static String longToString(long time) {
        Date date = new Date(time);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String newDate = sdf.format(date);
        return newDate;

    }

    /**
     *
     * @Title: DateToString
     * @Description:
     * @param time
     * @return:
     * @throws
     */
    public static String DateToString(Date time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String newDate = sdf.format(time);
        return newDate;
    }

    public static String DateToString(Date time, String formatter) {
        SimpleDateFormat sdf = new SimpleDateFormat(formatter);
        String newDate = sdf.format(time);
        return newDate;
    }

    /**
     *
     * @Title: parseDate
     * @Description: 把String解析格式的时间转化为date
     * @param stringTime
     * @return:
     * @throws
     */
    public static Date parseDate(String stringTime) {
        Date date = null;
        if (StringUtils.isNotBlank(stringTime)) {
            String[] pattern = new String[] { "yyyy年MM月dd日", "yyyy年MM月dd",
                    "yyyy-MM", "yyyy年MM月dd日", "yyyyMM", "yyyy/MM", "yyyyMMdd",
                    "yyyy-MM-dd", "yyyy/MM/dd", "yyyyMMddHHmmss",
                    "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm",
                    "yyyy/MM/dd HH:mm:ss",
                    "MM月dd日", "MM月dd",
                    "MM", "MM月dd日", "MM", "MMdd",
                    "MM-dd", "/MM/dd", "MMddHHmmss",
                    "MM-dd HH:mm:ss", "MM-dd HH:mm",
                    "MM/dd HH:mm:ss",
            };
            try {
                date = DateUtils.parseDate(stringTime, pattern);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return date;

    }

    /**
     *
     * @Description: 把String格式的时间转化为date
     * @param stringTime
     * @return:
     * @throws
     */
    public static Date formatDate(String stringTime) {
        Date date = null;
        if (StringUtils.isNotBlank(stringTime)) {
            String[] pattern = new String[] { "yyyy年MM月dd日HH时mm分ss秒",
                    "yyyy年MM月dd日 HH时mm分ss秒", "yyyy年MM月dd日HH时mm分",
                    "yyyy年MM月dd日 HH时mm分", "yyyy年MM月dd日 HH时", "yyyy年MM月dd日HH时",
                    "yyyy年MM月dd日", "yyyy年MM月dd日HH:mm:ss",
                    "yyyy年MM月dd日 HH:mm:ss", "yyyy年MM月dd日HH:mm",
                    "yyyy年MM月dd日 HH:mm", "yyyy年MM月dd日 HH", "yyyy年MM月dd日HH",
                    "yyyy-MM-dd HH时mm分ss秒", "yyyy-MM-dd HH时mm分",
                    "yyyy-MM-dd HH时", "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss",
                    "yyyy-MM-dd HH:mm", "yyyy-MM-dd HH",
                    "yyyy/MM/dd HH时mm分ss秒", "yyyy/MM/dd HH时mm分",
                    "yyyy/MM/dd HH时", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss",
                    "yyyy/MM/dd HH:mm", "yyyy/MM/dd HH", "yyyy.MM.dd HH:mm:ss",
                    "yyyy.MM.dd HH:mm", "yyyy.MM.dd HH", "yyyy.MM.dd",
                    "yyyyMMdd", };
            try {
                date = DateUtils.parseDate(stringTime, pattern);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return date;

    }

    /**
     *
     * @Description: 从字符串中截取出正确的时间
     * @param stringTime
     * @return:
     * @throws
     */
    public static Date cutDate(String stringTime) {
        String regs[] = { "d{4}年d{2}月d{2}日sd{2}时d{2}分d{2}秒",
                "d{4}年d{2}月d{2}日sd{1}时d{2}分d{2}秒",
                "d{4}年d{1}月d{2}日sd{1}时d{2}分d{2}秒",
                "d{4}年d{1}月d{2}日sd{2}时d{2}分d{2}秒",
                "d{4}年d{2}月d{2}日d{2}时d{2}分d{2}秒",
                "d{4}年d{2}月d{2}日sd{2}时d{2}分",
                "d{4}年d{1}月d{2}日sd{2}时d{2}分",
                "d{4}年d{1}月d{2}日sd{1}时d{2}分",
                "d{4}年d{1}月d{2}日sd{2}时d{2}分",
                "d{4}年d{2}月d{2}日d{2}时d{2}分",
                "d{4}年d{2}月d{2}日sd{2}时",
                "d{4}年d{2}月d{2}日sd{1}时",
                "d{4}年d{1}月d{2}日sd{2}时",
                "d{4}年d{1}月d{2}日sd{1}时",
                "d{4}年d{2}月d{2}日d{2}时", "d{4}年d{2}月d{2}日",
                "d{4}年d{2}月d{1}日", "d{4}年d{1}月d{2}日",
                "d{4}年d{1}月d{1}日",
                "d{4}年d{2}月d{2}日sd{2}:d{2}:d{2}",
                "d{4}年d{2}月d{2}日sd{2}:d{1}:d{2}",
                "d{4}年d{1}月d{2}日sd{2}:d{1}:d{2}",
                "d{4}年d{1}月d{2}日sd{2}:d{2}:d{2}",
                "d{4}年d{2}月d{2}日d{2}:d{2}:d{2}",
                "d{4}年d{2}月d{2}日sd{2}:d{2}",
                "d{4}年d{2}月d{2}日sd{1}:d{2}",
                "d{4}年d{1}月d{2}日sd{2}:d{2}",
                "d{4}年d{1}月d{2}日sd{1}:d{2}",
                "d{4}年d{2}月d{2}日d{2}:d{2}",
                "d{4}年d{2}月d{2}日sd{2}",
                "d{4}年d{2}月d{2}日sd{1}",
                "d{4}年d{1}月d{2}日sd{2}",
                "d{4}年d{1}月d{2}日sd{1}",
                "d{4}年d{2}月d{2}日d{2}",
                "d{4}-d{2}-d{2}sd{2}:d{2}:d{2}",
                "d{4}-d{2}-d{2}sd{1}:d{2}:d{2}",
                "d{4}-d{1}-d{2}sd{2}:d{2}:d{2}",
                "d{4}-d{1}-d{2}sd{1}:d{2}:d{2}",
                "d{4}-d{2}-d{2}sd{2}:d{2}",
                "d{4}-d{2}-d{2}sd{1}:d{2}",
                "d{4}-d{2}-d{2}sd{2}", "d{4}-d{2}-d{2}",
                "d{4}-d{2}-d{1}", "d{4}-d{1}-d{2}",
                "d{4}-d{1}-d{1}",
                "d{4}-d{2}-d{2}sd{2}时d{2}分d{2}秒",
                "d{4}-d{2}-d{2}sd{1}时d{2}分d{2}秒",
                "d{4}-d{1}-d{2}sd{2}时d{2}分d{2}秒",
                "d{4}-d{1}-d{2}sd{1}时d{2}分d{2}秒",
                "d{4}-d{1}-d{1}sd{1}时d{2}分d{2}秒",
                "d{4}-d{2}-d{2}sd{2}时d{2}分",
                "d{4}-d{2}-d{2}sd{1}时d{2}分",
                "d{4}-d{1}-d{2}sd{2}时d{2}分",
                "d{4}-d{1}-d{2}sd{1}时d{2}分",
                "d{4}-d{2}-d{2}sd{2}时",
                "d{4}-d{2}-d{2}sd{1}时",
                "d{4}-d{1}-d{2}sd{2}时",
                "d{4}-d{1}-d{2}sd{1}时", "d{4}.d{2}.d{2}",
                "d{4}.d{2}.d{1}", "d{4}.d{1}.d{2}",
                "d{4}.d{1}.d{1}",
                "d{4}.d{2}.d{2}sd{2}:d{2}:d{2}",
                "d{4}.d{2}.d{2}sd{1}:d{2}:d{2}",
                "d{4}.d{1}.d{2}sd{2}:d{2}:d{2}",
                "d{4}.d{1}.d{2}sd{1}:d{2}:d{2}",
                "d{4}.d{1}.d{1}sd{1}:d{2}:d{2}",
                "d{4}.d{2}.d{2}sd{2}:d{2}",
                "d{4}.d{2}.d{2}sd{1}:d{2}",
                "d{4}.d{1}.d{2}sd{2}:d{2}",
                "d{4}.d{1}.d{2}sd{1}:d{2}",
                "d{4}.d{2}.d{2}sd{2}",
                "d{4}.d{2}.d{2}sd{1}",
                "d{4}.d{1}.d{2}sd{2}",
                "d{4}.d{1}.d{2}sd{1}",
                "d{4}/d{2}/d{2}sd{2}时d{2}分d{2}秒",
                "d{4}/d{2}/d{2}sd{1}时d{2}分d{2}秒",
                "d{4}/d{1}/d{2}sd{2}时d{2}分d{2}秒",
                "d{4}/d{1}/d{2}sd{1}时d{2}分d{2}秒",
                "d{4}/d{2}/d{2}sd{2}时d{2}分",
                "d{4}/d{2}/d{2}sd{1}时d{2}分",
                "d{4}/d{1}/d{2}sd{2}时d{2}分",
                "d{4}/d{1}/d{2}sd{1}时d{2}分",
                "d{4}/d{2}/d{2}sd{2}时",
                "d{4}/d{2}/d{2}sd{1}时",
                "d{4}/d{1}/d{2}sd{2}时",
                "d{4}/d{1}/d{2}sd{1}时",
                "d{4}/d{2}/d{2}",
                "d{4}/d{2}/d{1}",
                "d{4}/d{1}/d{2}",
                "d{4}/d{1}/d{1}",
                "d{4}/d{2}/d{2}sd{2}:d{2}:d{2}",
                "d{4}/d{2}/d{2}sd{1}:d{2}:d{2}",
                "d{4}/d{1}/d{2}sd{2}:d{2}:d{2}",
                "d{4}/d{1}/d{2}sd{1}:d{2}:d{2}",
                "d{4}/d{2}/d{2}sd{2}:d{2}",
                "d{4}/d{2}/d{2}sd{1}:d{2}",
                "d{4}/d{1}/d{2}sd{2}:d{2}",
                "d{4}/d{1}/d{2}sd{1}:d{2}",
                "d{4}/d{2}/d{2}sd{2}",
                "d{4}/d{2}/d{2}sd{1}",
                "d{4}/d{1}/d{2}sd{2}",
                "d{4}/d{1}/d{2}sd{1}",
                "d{2}月d{2}日sd{2}时d{2}分d{2}秒",
                "d{2}月d{2}日sd{1}时d{2}分d{2}秒",
                "d{1}月d{2}日sd{1}时d{2}分d{2}秒",
                "d{1}月d{2}日sd{2}时d{2}分d{2}秒",
                "d{2}月d{2}日d{2}时d{2}分d{2}秒",
                "d{2}月d{2}日sd{2}时d{2}分",
                "d{1}月d{2}日sd{2}时d{2}分",
                "d{1}月d{2}日sd{1}时d{2}分",
                "d{1}月d{2}日sd{2}时d{2}分",
                "d{2}月d{2}日d{2}时d{2}分",
                "d{2}月d{2}日sd{2}时",
                "d{2}月d{2}日sd{1}时",
                "d{1}月d{2}日sd{2}时",
                "d{1}月d{2}日sd{1}时",
                "d{2}月d{2}日d{2}时", "d{4}年d{2}月d{2}日",
                "d{2}月d{1}日", "d{4}年d{1}月d{2}日",
                "d{1}月d{1}日",
                "d{2}月d{2}日sd{2}:d{2}:d{2}",
                "d{2}月d{2}日sd{2}:d{1}:d{2}",
                "d{1}月d{2}日sd{2}:d{1}:d{2}",
                "d{1}月d{2}日sd{2}:d{2}:d{2}",
                "d{2}月d{2}日d{2}:d{2}:d{2}",
                "d{2}月d{2}日sd{2}:d{2}",
                "d{2}月d{2}日sd{1}:d{2}",
                "d{1}月d{2}日sd{2}:d{2}",
                "d{1}月d{2}日sd{1}:d{2}",
                "d{2}月d{2}日d{2}:d{2}",
                "d{2}月d{2}日sd{2}",
                "d{2}月d{2}日sd{1}",
                "d{1}月d{2}日sd{2}",
                "d{1}月d{2}日sd{1}",
                "d{2}月d{2}日d{2}",
                "d{2}-d{2}sd{2}:d{2}:d{2}",
                "d{2}-d{2}sd{1}:d{2}:d{2}",
                "d{1}-d{2}sd{2}:d{2}:d{2}",
                "d{1}-d{2}sd{1}:d{2}:d{2}",
                "d{2}-d{2}sd{2}:d{2}",
                "d{2}-d{2}sd{1}:d{2}",
                "d{2}-d{2}sd{2}", "d{4}-d{2}-d{2}",
                "d{2}-d{1}", "d{4}-d{1}-d{2}",
                "d{1}-d{1}",
                "d{2}-d{2}sd{2}时d{2}分d{2}秒",
                "d{2}-d{2}sd{1}时d{2}分d{2}秒",
                "d{1}-d{2}sd{2}时d{2}分d{2}秒",
                "d{1}-d{2}sd{1}时d{2}分d{2}秒",
                "d{1}-d{1}sd{1}时d{2}分d{2}秒",
                "d{2}-d{2}sd{2}时d{2}分",
                "d{2}-d{2}sd{1}时d{2}分",
                "d{1}-d{2}sd{2}时d{2}分",
                "d{1}-d{2}sd{1}时d{2}分",
                "d{2}-d{2}sd{2}时",
                "d{2}-d{2}sd{1}时",
                "d{1}-d{2}sd{2}时",
                "d{1}-d{2}sd{1}时", "d{4}.d{2}.d{2}",
                "d{2}.d{1}", "d{4}.d{1}.d{2}",
                "d{1}.d{1}",
                "d{2}.d{2}sd{2}:d{2}:d{2}",
                "d{2}.d{2}sd{1}:d{2}:d{2}",
                "d{1}.d{2}sd{2}:d{2}:d{2}",
                "d{1}.d{2}sd{1}:d{2}:d{2}",
                "d{1}.d{1}sd{1}:d{2}:d{2}",
                "d{2}.d{2}sd{2}:d{2}",
                "d{2}.d{2}sd{1}:d{2}",
                "d{1}.d{2}sd{2}:d{2}",
                "d{1}.d{2}sd{1}:d{2}",
                "d{2}.d{2}sd{2}",
                "d{2}.d{2}sd{1}",
                "d{1}.d{2}sd{2}",
                "d{1}.d{2}sd{1}",
                "d{2}/d{2}sd{2}时d{2}分d{2}秒",
                "d{2}/d{2}sd{1}时d{2}分d{2}秒",
                "d{1}/d{2}sd{2}时d{2}分d{2}秒",
                "d{1}/d{2}sd{1}时d{2}分d{2}秒",
                "d{2}/d{2}sd{2}时d{2}分",
                "d{2}/d{2}sd{1}时d{2}分",
                "d{1}/d{2}sd{2}时d{2}分",
                "d{1}/d{2}sd{1}时d{2}分",
                "d{2}/d{2}sd{2}时",
                "d{2}/d{2}sd{1}时",
                "d{1}/d{2}sd{2}时",
                "d{1}/d{2}sd{1}时",
                "d{2}/d{2}",
                "d{2}/d{1}",
                "d{1}/d{2}",
                "d{1}/d{1}",
                "d{2}/d{2}sd{2}:d{2}:d{2}",
                "d{2}/d{2}sd{1}:d{2}:d{2}",
                "d{1}/d{2}sd{2}:d{2}:d{2}",
                "d{1}/d{2}sd{1}:d{2}:d{2}",
                "d{2}/d{2}sd{2}:d{2}",
                "d{2}/d{2}sd{1}:d{2}",
                "d{1}/d{2}sd{2}:d{2}",
                "d{1}/d{2}sd{1}:d{2}",
                "d{2}/d{2}sd{2}",
                "d{2}/d{2}sd{1}",
                "d{1}/d{2}sd{2}",
                "d{1}/d{2}sd{1}",
        };

        String str = "";
        Date date = null;
        for (String reg : regs) {
            String temp = match(reg, stringTime);
            if (temp.length() > str.length()) {
                str = temp;
                if (!"".equals(str)) {
                    // System.err.println(reg);
                    date = formatDate(str);
                }
            }

        }
        return date;

    }

    public static String match(String reg, String stringTime) {
        Pattern p = Pattern.compile(reg);
        Matcher m = p.matcher(stringTime);
        String s = "";
        if (m.find()) {
            s += m.group();
        }
        return s;
    }

    public static String addMonth(String time) {

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date date = parseDate(time);
        date = DateUtils.addDays(date, 1);
        String first = df.format(date);
        return first;
    }



    /*
     * public static void main(String[] args) { // String
     * str="Thu Sep 08 2011 00:00:00 GMT+0800 "; String
     * str="Thu May 28 14:45:43 +0800 2015"; SimpleDateFormat sdf=new
     * SimpleDateFormat("EEE MMM dd HH:mm:ss +0800 yyyy",Locale.ENGLISH); try {
     * Date date = sdf.parse(str); String time = DateToString(date);
     * System.err.println(date); System.err.println(time); } catch
     * (ParseException e) { e.printStackTrace(); } }
     */
    public static void main(String[] args) {
        String stringTime = "2021/1/2 9:20:35 2021/1/2 21:20:35";
        Date date = cutDate(stringTime);
        System.out.println(date);
        System.out.println(DateToString(date));
    }
}



知识共享许可协议本作品采用知识共享署名-相同方式共享 4.0 国际许可协议进行许可。

相关推荐

暂无评论