如何从字符串里面提取时间
今天接到一个需求,需要从一段带有时间串的字符串里面提取出时间保存。
"PDFS(iZh9k5gurgwcwpZ) 2020-01-01 21:27:36.560 >订单编号:1112233 提交订单成功! 跟踪号:待取跟踪号 内部单号:2342244"
需要从上面数据中心提取出 2020-01-01 21:27:36要怎么操作呢?
我们可以使用正则表达式
对时间串进行提取,并处理,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
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;
/**
* Created by chpeng on 2020/08/18 0008.
*/
public class StringDateUtils {
/**
*
* @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{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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{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\\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;
}
/**
*
* @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;
}
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;
}
/**
* 格式化
* @Title: DateToString
* @Description:
* @param time
* @return:
* @throws
*/
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之间的所有年份
正则表达式
常用正则表达式:
-
\d
: 数字字符匹配。等效于 [0-9] -
\D
: 非数字字符匹配。等效于 [^0-9]。 -
{n}
: n为非负整数,正好匹配n次 -
{n,}
: n为非负整数,至少匹配n次 -
[a-z]
: 字符范围。匹配指定范围内的任何字符,例如,"[a-z]"匹配"a"到"z"范围内的任何小写字母。 -
[^a-z]
: 反向范围字符。匹配不在指定的范围内的任何字符。例如,"[^a-z]"匹配任何不在"a"到"z"范围内的任何字符。 -
\
: 将下一字符标记为特殊字符、文本、反向引用或八进制转义符 -
^
: 匹配输入字符串开始的位置 -
$
: 匹配输入字符串结尾的位置。 -
*
: 零次或多次匹配前面的字符或子表达式。例如,zo* 匹配"z"和"zoo"。* 等效于 {0,} -
+
: 一次或多次匹配前面的字符或子表达式。例如,"zo+"与"zo"和"zoo"匹配,但与"z"不匹配。+ 等效于 {1,}。 -
?
: 零次或一次匹配前面的字符或子表达式。例如,"do(es)?"匹配"do"或"does"中的"do"。? 等效于 {0,1}。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
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 ;
}
}
|
传统匹配:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
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 ;
}
}
|
以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/flycp/article/details/108141694