Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6978777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:48:00+00:00 2026-05-27T17:48:00+00:00

I want to split a string coming from a log file, format the split

  • 0

I want to split a string coming from a log file, format the split string, concatenate it again, then print the formatted string again. Currently, the code is just printing file line by line while the rest that I want is remaining.

My code is:

   try                
   {
      String strpath="/var/date.log";
      FileReader fr = new FileReader(strpath);
      BufferedReader br = new BufferedReader(fr);
      String ch;
      String[] Arr;
      do 
      {
        ch = br.readLine();
        if (ch != null)
        out.print(ch+"<br/>");   
      } 
      while (ch != null);
      fr.close();
    }
    catch(IOException e){
    out.print(e.getMessage());
  }

Contents of log file:

[1322110800] LOG ROTATION: DAILY
[1322110800] LOG VERSION: 2.0
[1322110800] CURRENT HOST STATE: arsalan.hussain;DOWN;HARD;1;CRITICAL - Host Unreachable (192.168.1.107)
[1322110800] CURRENT HOST STATE: localhost;UP;HARD;1;PING OK - Packet loss = 0%, RTA = 0.06 ms
[1322110800] CURRENT HOST STATE: musewerx-72c7b0;UP;HARD;1;PING OK - Packet loss = 0%, RTA = 0.27 ms
[1322110800] CURRENT HOST STATE: sharepoint2;DOWN;HARD;1;CRITICAL - Host Unreachable (192.168.1.100)

I want to split this file per line on “]” and then get content in square brackets, format it in long date time and then concatenate it with the remaining line and print it.

This should be done with every line of the file.

This will give the formatted date:

String dat="1324649468000";
Date d = new Date(Long.valueOf(dat));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:48:01+00:00Added an answer on May 27, 2026 at 5:48 pm

    You can use this simple code snippet to do that conversion:

    String line = "[1322110800] LOG ROTATION: DAILY";
    Pattern pt = Pattern.compile("\\[(\\d+)\\]");
    Matcher m = pt.matcher(line);
    if (m.find()) {
        Date dt = new Date(Long.parseLong(m.group(1)) * 1000);
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
        line = m.replaceFirst('['+ sdf.format(dt) +']');
    }
    
    System.out.println(line);
    

    OUTPUT

    [11-24-2011 00:00:00] LOG ROTATION: DAILY
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to split the /n from my string in Java. For example, I
I want to split a string like this: abc//def//ghi into a part before and
I want to split a string like 001A into 001 and A
I have a string ABCD:10,20,,40;1/1;1/2,1/3,1/4 I want to split the string into the following
Really simple problem: I want to split a connection string into its keyword /
Using Java (1.6) I want to split an input string that has components of
i'm amol kadam,I want to know how to split string in two part.My string
Unfortunately, there seems to be no string.Split(string separator), only string.Split(char speparator). I want to
I want to split a command line like string in single string parameters. How
I have the following string 3/4Ton. I want to split it as --> word[1]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.