简体   繁体   中英

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

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:

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());
            }
     }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM