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}日\\s\\d{2}时\\d{2}分\\d{2}秒",
                "\\d{4}年\\d{2}月\\d{2}日\\s\\d{1}时\\d{2}分\\d{2}秒",
                "\\d{4}年\\d{1}月\\d{2}日\\s\\d{1}时\\d{2}分\\d{2}秒",
                "\\d{4}年\\d{1}月\\d{2}日\\s\\d{2}时\\d{2}分\\d{2}秒",
                "\\d{4}年\\d{2}月\\d{2}日\\d{2}时\\d{2}分\\d{2}秒",
                "\\d{4}年\\d{2}月\\d{2}日\\s\\d{2}时\\d{2}分",
                "\\d{4}年\\d{1}月\\d{2}日\\s\\d{2}时\\d{2}分",
                "\\d{4}年\\d{1}月\\d{2}日\\s\\d{1}时\\d{2}分",
                "\\d{4}年\\d{1}月\\d{2}日\\s\\d{2}时\\d{2}分",
                "\\d{4}年\\d{2}月\\d{2}日\\d{2}时\\d{2}分",
                "\\d{4}年\\d{2}月\\d{2}日\\s\\d{2}时",
                "\\d{4}年\\d{2}月\\d{2}日\\s\\d{1}时",
                "\\d{4}年\\d{1}月\\d{2}日\\s\\d{2}时",
                "\\d{4}年\\d{1}月\\d{2}日\\s\\d{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}日\\s\\d{2}:\\d{2}:\\d{2}",
                "\\d{4}年\\d{2}月\\d{2}日\\s\\d{2}:\\d{1}:\\d{2}",
                "\\d{4}年\\d{1}月\\d{2}日\\s\\d{2}:\\d{1}:\\d{2}",
                "\\d{4}年\\d{1}月\\d{2}日\\s\\d{2}:\\d{2}:\\d{2}",
                "\\d{4}年\\d{2}月\\d{2}日\\d{2}:\\d{2}:\\d{2}",
                "\\d{4}年\\d{2}月\\d{2}日\\s\\d{2}:\\d{2}",
                "\\d{4}年\\d{2}月\\d{2}日\\s\\d{1}:\\d{2}",
                "\\d{4}年\\d{1}月\\d{2}日\\s\\d{2}:\\d{2}",
                "\\d{4}年\\d{1}月\\d{2}日\\s\\d{1}:\\d{2}",
                "\\d{4}年\\d{2}月\\d{2}日\\d{2}:\\d{2}",
                "\\d{4}年\\d{2}月\\d{2}日\\s\\d{2}",
                "\\d{4}年\\d{2}月\\d{2}日\\s\\d{1}",
                "\\d{4}年\\d{1}月\\d{2}日\\s\\d{2}",
                "\\d{4}年\\d{1}月\\d{2}日\\s\\d{1}",
                "\\d{4}年\\d{2}月\\d{2}日\\d{2}",
                "\\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}",
                "\\d{4}-\\d{2}-\\d{2}\\s\\d{1}:\\d{2}:\\d{2}",
                "\\d{4}-\\d{1}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}",
                "\\d{4}-\\d{1}-\\d{2}\\s\\d{1}:\\d{2}:\\d{2}",
                "\\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}",
                "\\d{4}-\\d{2}-\\d{2}\\s\\d{1}:\\d{2}",
                "\\d{4}-\\d{2}-\\d{2}\\s\\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}\\s\\d{2}时\\d{2}分\\d{2}秒",
                "\\d{4}-\\d{2}-\\d{2}\\s\\d{1}时\\d{2}分\\d{2}秒",
                "\\d{4}-\\d{1}-\\d{2}\\s\\d{2}时\\d{2}分\\d{2}秒",
                "\\d{4}-\\d{1}-\\d{2}\\s\\d{1}时\\d{2}分\\d{2}秒",
                "\\d{4}-\\d{1}-\\d{1}\\s\\d{1}时\\d{2}分\\d{2}秒",
                "\\d{4}-\\d{2}-\\d{2}\\s\\d{2}时\\d{2}分",
                "\\d{4}-\\d{2}-\\d{2}\\s\\d{1}时\\d{2}分",
                "\\d{4}-\\d{1}-\\d{2}\\s\\d{2}时\\d{2}分",
                "\\d{4}-\\d{1}-\\d{2}\\s\\d{1}时\\d{2}分",
                "\\d{4}-\\d{2}-\\d{2}\\s\\d{2}时",
                "\\d{4}-\\d{2}-\\d{2}\\s\\d{1}时",
                "\\d{4}-\\d{1}-\\d{2}\\s\\d{2}时",
                "\\d{4}-\\d{1}-\\d{2}\\s\\d{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}\\s\\d{2}:\\d{2}:\\d{2}",
                "\\d{4}.\\d{2}.\\d{2}\\s\\d{1}:\\d{2}:\\d{2}",
                "\\d{4}.\\d{1}.\\d{2}\\s\\d{2}:\\d{2}:\\d{2}",
                "\\d{4}.\\d{1}.\\d{2}\\s\\d{1}:\\d{2}:\\d{2}",
                "\\d{4}.\\d{1}.\\d{1}\\s\\d{1}:\\d{2}:\\d{2}",
                "\\d{4}.\\d{2}.\\d{2}\\s\\d{2}:\\d{2}",
                "\\d{4}.\\d{2}.\\d{2}\\s\\d{1}:\\d{2}",
                "\\d{4}.\\d{1}.\\d{2}\\s\\d{2}:\\d{2}",
                "\\d{4}.\\d{1}.\\d{2}\\s\\d{1}:\\d{2}",
                "\\d{4}.\\d{2}.\\d{2}\\s\\d{2}",
                "\\d{4}.\\d{2}.\\d{2}\\s\\d{1}",
                "\\d{4}.\\d{1}.\\d{2}\\s\\d{2}",
                "\\d{4}.\\d{1}.\\d{2}\\s\\d{1}",
                "\\d{4}/\\d{2}/\\d{2}\\s\\d{2}时\\d{2}分\\d{2}秒",
                "\\d{4}/\\d{2}/\\d{2}\\s\\d{1}时\\d{2}分\\d{2}秒",
                "\\d{4}/\\d{1}/\\d{2}\\s\\d{2}时\\d{2}分\\d{2}秒",
                "\\d{4}/\\d{1}/\\d{2}\\s\\d{1}时\\d{2}分\\d{2}秒",
                "\\d{4}/\\d{2}/\\d{2}\\s\\d{2}时\\d{2}分",
                "\\d{4}/\\d{2}/\\d{2}\\s\\d{1}时\\d{2}分",
                "\\d{4}/\\d{1}/\\d{2}\\s\\d{2}时\\d{2}分",
                "\\d{4}/\\d{1}/\\d{2}\\s\\d{1}时\\d{2}分",
                "\\d{4}/\\d{2}/\\d{2}\\s\\d{2}时",
                "\\d{4}/\\d{2}/\\d{2}\\s\\d{1}时",
                "\\d{4}/\\d{1}/\\d{2}\\s\\d{2}时",
                "\\d{4}/\\d{1}/\\d{2}\\s\\d{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}\\s\\d{2}:\\d{2}:\\d{2}",
                "\\d{4}/\\d{2}/\\d{2}\\s\\d{1}:\\d{2}:\\d{2}",
                "\\d{4}/\\d{1}/\\d{2}\\s\\d{2}:\\d{2}:\\d{2}",
                "\\d{4}/\\d{1}/\\d{2}\\s\\d{1}:\\d{2}:\\d{2}",
                "\\d{4}/\\d{2}/\\d{2}\\s\\d{2}:\\d{2}",
                "\\d{4}/\\d{2}/\\d{2}\\s\\d{1}:\\d{2}",
                "\\d{4}/\\d{1}/\\d{2}\\s\\d{2}:\\d{2}",
                "\\d{4}/\\d{1}/\\d{2}\\s\\d{1}:\\d{2}",
                "\\d{4}/\\d{2}/\\d{2}\\s\\d{2}",
                "\\d{4}/\\d{2}/\\d{2}\\s\\d{1}",
                "\\d{4}/\\d{1}/\\d{2}\\s\\d{2}",
                "\\d{4}/\\d{1}/\\d{2}\\s\\d{1}",
                "\\d{2}月\\d{2}日\\s\\d{2}时\\d{2}分\\d{2}秒",
                "\\d{2}月\\d{2}日\\s\\d{1}时\\d{2}分\\d{2}秒",
                "\\d{1}月\\d{2}日\\s\\d{1}时\\d{2}分\\d{2}秒",
                "\\d{1}月\\d{2}日\\s\\d{2}时\\d{2}分\\d{2}秒",
                "\\d{2}月\\d{2}日\\d{2}时\\d{2}分\\d{2}秒",
                "\\d{2}月\\d{2}日\\s\\d{2}时\\d{2}分",
                "\\d{1}月\\d{2}日\\s\\d{2}时\\d{2}分",
                "\\d{1}月\\d{2}日\\s\\d{1}时\\d{2}分",
                "\\d{1}月\\d{2}日\\s\\d{2}时\\d{2}分",
                "\\d{2}月\\d{2}日\\d{2}时\\d{2}分",
                "\\d{2}月\\d{2}日\\s\\d{2}时",
                "\\d{2}月\\d{2}日\\s\\d{1}时",
                "\\d{1}月\\d{2}日\\s\\d{2}时",
                "\\d{1}月\\d{2}日\\s\\d{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}日\\s\\d{2}:\\d{2}:\\d{2}",
                "\\d{2}月\\d{2}日\\s\\d{2}:\\d{1}:\\d{2}",
                "\\d{1}月\\d{2}日\\s\\d{2}:\\d{1}:\\d{2}",
                "\\d{1}月\\d{2}日\\s\\d{2}:\\d{2}:\\d{2}",
                "\\d{2}月\\d{2}日\\d{2}:\\d{2}:\\d{2}",
                "\\d{2}月\\d{2}日\\s\\d{2}:\\d{2}",
                "\\d{2}月\\d{2}日\\s\\d{1}:\\d{2}",
                "\\d{1}月\\d{2}日\\s\\d{2}:\\d{2}",
                "\\d{1}月\\d{2}日\\s\\d{1}:\\d{2}",
                "\\d{2}月\\d{2}日\\d{2}:\\d{2}",
                "\\d{2}月\\d{2}日\\s\\d{2}",
                "\\d{2}月\\d{2}日\\s\\d{1}",
                "\\d{1}月\\d{2}日\\s\\d{2}",
                "\\d{1}月\\d{2}日\\s\\d{1}",
                "\\d{2}月\\d{2}日\\d{2}",
                "\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}",
                "\\d{2}-\\d{2}\\s\\d{1}:\\d{2}:\\d{2}",
                "\\d{1}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}",
                "\\d{1}-\\d{2}\\s\\d{1}:\\d{2}:\\d{2}",
                "\\d{2}-\\d{2}\\s\\d{2}:\\d{2}",
                "\\d{2}-\\d{2}\\s\\d{1}:\\d{2}",
                "\\d{2}-\\d{2}\\s\\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}\\s\\d{2}时\\d{2}分\\d{2}秒",
                "\\d{2}-\\d{2}\\s\\d{1}时\\d{2}分\\d{2}秒",
                "\\d{1}-\\d{2}\\s\\d{2}时\\d{2}分\\d{2}秒",
                "\\d{1}-\\d{2}\\s\\d{1}时\\d{2}分\\d{2}秒",
                "\\d{1}-\\d{1}\\s\\d{1}时\\d{2}分\\d{2}秒",
                "\\d{2}-\\d{2}\\s\\d{2}时\\d{2}分",
                "\\d{2}-\\d{2}\\s\\d{1}时\\d{2}分",
                "\\d{1}-\\d{2}\\s\\d{2}时\\d{2}分",
                "\\d{1}-\\d{2}\\s\\d{1}时\\d{2}分",
                "\\d{2}-\\d{2}\\s\\d{2}时",
                "\\d{2}-\\d{2}\\s\\d{1}时",
                "\\d{1}-\\d{2}\\s\\d{2}时",
                "\\d{1}-\\d{2}\\s\\d{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}\\s\\d{2}:\\d{2}:\\d{2}",
                "\\d{2}.\\d{2}\\s\\d{1}:\\d{2}:\\d{2}",
                "\\d{1}.\\d{2}\\s\\d{2}:\\d{2}:\\d{2}",
                "\\d{1}.\\d{2}\\s\\d{1}:\\d{2}:\\d{2}",
                "\\d{1}.\\d{1}\\s\\d{1}:\\d{2}:\\d{2}",
                "\\d{2}.\\d{2}\\s\\d{2}:\\d{2}",
                "\\d{2}.\\d{2}\\s\\d{1}:\\d{2}",
                "\\d{1}.\\d{2}\\s\\d{2}:\\d{2}",
                "\\d{1}.\\d{2}\\s\\d{1}:\\d{2}",
                "\\d{2}.\\d{2}\\s\\d{2}",
                "\\d{2}.\\d{2}\\s\\d{1}",
                "\\d{1}.\\d{2}\\s\\d{2}",
                "\\d{1}.\\d{2}\\s\\d{1}",
                "\\d{2}/\\d{2}\\s\\d{2}时\\d{2}分\\d{2}秒",
                "\\d{2}/\\d{2}\\s\\d{1}时\\d{2}分\\d{2}秒",
                "\\d{1}/\\d{2}\\s\\d{2}时\\d{2}分\\d{2}秒",
                "\\d{1}/\\d{2}\\s\\d{1}时\\d{2}分\\d{2}秒",
                "\\d{2}/\\d{2}\\s\\d{2}时\\d{2}分",
                "\\d{2}/\\d{2}\\s\\d{1}时\\d{2}分",
                "\\d{1}/\\d{2}\\s\\d{2}时\\d{2}分",
                "\\d{1}/\\d{2}\\s\\d{1}时\\d{2}分",
                "\\d{2}/\\d{2}\\s\\d{2}时",
                "\\d{2}/\\d{2}\\s\\d{1}时",
                "\\d{1}/\\d{2}\\s\\d{2}时",
                "\\d{1}/\\d{2}\\s\\d{1}时",
                "\\d{2}/\\d{2}",
                "\\d{2}/\\d{1}",
                "\\d{1}/\\d{2}",
                "\\d{1}/\\d{1}",
                "\\d{2}/\\d{2}\\s\\d{2}:\\d{2}:\\d{2}",
                "\\d{2}/\\d{2}\\s\\d{1}:\\d{2}:\\d{2}",
                "\\d{1}/\\d{2}\\s\\d{2}:\\d{2}:\\d{2}",
                "\\d{1}/\\d{2}\\s\\d{1}:\\d{2}:\\d{2}",
                "\\d{2}/\\d{2}\\s\\d{2}:\\d{2}",
                "\\d{2}/\\d{2}\\s\\d{1}:\\d{2}",
                "\\d{1}/\\d{2}\\s\\d{2}:\\d{2}",
                "\\d{1}/\\d{2}\\s\\d{1}:\\d{2}",
                "\\d{2}/\\d{2}\\s\\d{2}",
                "\\d{2}/\\d{2}\\s\\d{1}",
                "\\d{1}/\\d{2}\\s\\d{2}",
                "\\d{1}/\\d{2}\\s\\d{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));
    }
}
点赞 0

Saiyintai

软件测试工程师

暂无评论

相关推荐

Java 项目所需的包

包名描述所属层次xx.xx.domain存放系统的JavaBean类(只包含简单的属性以及属性对应的get和set方法,不包含具体的业务处理方 …

Idea 添加Java web 项目

1)新建项目,选择Java Enterprse 2)相关配置 配置JDK Project template 选择Web application Application server 选择Tom …

微信扫一扫,分享到朋友圈

Java 从String类型数据获取时间