文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

java 如何从字符串里面提取时间

2024-04-02 19:55

关注

如何从字符串里面提取时间

今天接到一个需求,需要从一段带有时间串的字符串里面提取出时间保存。

"PDFS(iZh9k5gurgwcwpZ) 2020-01-01 21:27:36.560 >订单编号:1112233 提交订单成功! 跟踪号:待取跟踪号 内部单号:2342244"

需要从上面数据中心提取出 2020-01-01 21:27:36要怎么操作呢?

我们可以使用正则表达式

对时间串进行提取,并处理,代码如下:


package com.epean.trade.framework.util;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;
import java.text.ParseException;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringDateUtils {
	
	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)) {
					date = formatDate(str);
				}
			}
		}
		return date;
	}
	
	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;
	}
	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 DateToString(Date time) {
		String newDate = DateFormatUtils.format(time,"yyyy-MM-dd HH:mm:ss");
		return newDate;
	}
	
    public static void main(String[] args) {
		String stringTime = "PDFS(iZh9k5gurgwcwpZ) 2020-01-01 21:27:36.560 >订单编号:1112233   提交订单成功!   跟踪号:待取跟踪号   内部单号:2342244";
		Date date = cutDate(stringTime);
		System.out.println(date);
		System.out.println(DateToString(date));
	}
}

我们测试一下:

在这里插入图片描述

没问题的。

从字符串中提取年份,要求输出1000-4999之间的所有年份

正则表达式

常用正则表达式:


import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class JingDong {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        String reg = "\\D[1-4]\\d{3}\\D";//eg:a2019a、a2019、2019a
      // 前面不能是数字,后面也不能是数字
        str = str +"a";
        str = "a" + str;
        Pattern p = Pattern.compile(reg);
        Matcher matcher = p.matcher(str);
       while(matcher.find()){
            System.out.print(matcher.group(0).substring(1,5) + " ");
        }
    }
    public static boolean isNum(char a){
        if(Character.isDigit(a)){
            return true;
        }else
            return false;
    }
}

传统匹配:


import java.util.*;
public class JingDong {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
          String str = sc.nextLine();
        if(str.length()<=2000){
            String out = "";
            for (int i = 0; i < str.length()-1;) {
                if(isNum(str.charAt(i) )&& Integer.parseInt(String.valueOf(str.charAt(i))) <=4 && Integer.parseInt(String.valueOf(str.charAt(i)))>=1){
                    if(isNum(str.charAt(i+1))){
                        if(isNum(str.charAt(i+2))){
                            if(isNum(str.charAt(i+3))){
                                if (!isNum(str.charAt(i+4))){
                                    out += str.substring(i,i+4);
                                    out += " ";
                                    i+=4;
                                }
                                i+=4;
                            }else
                            i++;
                        }else
                         i++;
                    }else
                        i++;
                }else
                    i++;
            }
            System.out.println(out);
        }
    public static boolean isNum(char a){
        if(Character.isDigit(a)){
            return true;
        }else
            return false;
    }
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯