[英]How can I only retrieve the date? Java
I read it from the logfile but there is something like我从日志文件中读取它,但有类似的东西
2021-04-29 18:16:30.038 [POOL-5-THREAD-1] BATCH ISSUE POLICY END TO END|START:2021-04-29 18:16:30|END:2021-04-29 18:16:30|RES:10MS
I will just take 2021-04-29我只会拿 2021-04-29
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class Main{
public static void main (String[] args)throws Exception{
demoReadall();
}
public static void demoReadall(){
try{
FileInputStream fstream = new FileInputStream("testlog.log");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
/* read log line by line */
while ((strLine = br.readLine()) != null) {
/* parse strLine to obtain what you want */
System.out.println (strLine);
}
fstream.close();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
}
thank you.谢谢你。
Do you mean this?你是这个意思吗?
Regards问候
Output: Output:
2021-04-29
2021-04-29
2021-04-29
Code:代码:
import java.util.*;
import java.lang.*;
import java.util.regex.*;
public class Main {
public static void main(String args[]) {
Pattern pattern = Pattern.compile("(\\d{4}-\\d{2}-\\d{2})");
Matcher matcher = pattern.matcher("2021-04-29 18:16:30.038 [POOL-5-THREAD-1] BATCH ISSUE POLICY END TO END|START:2021-04-29 18:16:30|END:2021-04-29 18:16:30|RES:10MS");
while (matcher.find()) {
String match = matcher.group();
System.out.println(match);
}
}
}
In your code, it would look as follows:在您的代码中,它将如下所示:
import java.util.*;
import java.lang.*;
import java.util.regex.*;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class Main{
public static void main (String[] args)throws Exception{
demoReadall();
}
public static void demoReadall(){
try{
FileInputStream fstream = new FileInputStream("testlog.log");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
Pattern pattern = Pattern.compile("(\\d{4}-\\d{2}-\\d{2})");
Matcher matcher;
/* read log line by line */
while ((strLine = br.readLine()) != null) {
matcher = pattern.matcher(strLine);
while (matcher.find()) {
String match = matcher.group();
System.out.println(match);
}
/* parse strLine to obtain what you want */
//System.out.println (strLine);
}
fstream.close();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.