File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes Unix Time Conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Unix Time Conversion" Watch "Unix Time Conversion" New topic
Author

Unix Time Conversion

Chuck Patterson
Greenhorn

Joined: May 25, 2004
Posts: 8
I am a recently SCJP - November, 2003. The company that I work for does a lot of our development work on Unix servers and ocassionaly run into some problems during this development process which is documented into certain types of "log files". Unfortunately, these files are displayed with a Unix time stamp that is a string of letters and numbers that don�t display the actual time and thus makes it difficult to search for the exact time occurrence of the problems. I have been given the following assignment:

1.Create a command line utility that will accept one of the log files � system_log, status_log or all_jobs_log files with the Unix time stamp and convert the �slash� /t�s to tabs and reformat the converted file to human time in columns showing the event generator, time and the message. A command line switch will direct the utility either to display the log file in a GUI or write it out to a text file.
2.Include in this utility the ability to display a File Browser with the option to select multiple versions of the same log file to view.
3.Also include in this utility the ability to sort the columns in ascending or descending order of the log files that are displayed in the GUI and also have the option to save that file to a text file in the format that it is displayed in from the File Browser.

An example of the log file is as follows;

JPM \t1080593081\t518:3:417\t496:30:147\t502:9:15\t532:9:1\t602:27:XDSVP_514\t546:27 iagnostics\t503:27:XDJ_559714\t518:20:0\t496:20:0\t

Can anyone PLEASE give me some pointers on how to get started on this or some suggestions, coding sequences or anything that will help me because I don't know where I should start first, thanks.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Hi Chuck

Welcome to JavaRanch!

No idea what you mean by "UNIX time stamp" that's comprised of alphanumerics. Looking at the sample line, I belive the second field, 1080593081, is the UNIX time. It's seconds since midnight, January 1st, 1970; the given value falls on March 29th of this year. Luckily, Java uses UNIX time also; you can make a lovely printable timestamp by multiplying this value by 1000, and then using it as the constructor argument to java.util.Date; i.e.,

System.out.println(new java.util.Date(1080593081L * 1000L));

prints

Mon Mar 29 15:44:41 EST 2004

Note that the result of the multiply has to be a long, or it will wrap around.

So what you want to do, probably, is just to use the String.split() method to break each line into tokens, then re-emit the lines with the tabs as desired. For displaying the sortable columns, you'll obviously use a JTable; but you need to start by parsing the data, first.


[Jess in Action][AskingGoodQuestions]
Chuck Patterson
Greenhorn

Joined: May 25, 2004
Posts: 8
Thank you very much for the suggestions. This is the start of the my coding;

code

//UnixTime


import java.io.*;
import java.util.*;



public class UnixTime {

public static void main(String[] args) throws IOException {
File inputFile = new File("Unix_in.txt");
File outputFile = new File("Unix_out.txt");

FileReader in = new FileReader(inputFile);
FileWriter out = new FileWriter(outputFile);

Long CurrTime;
StringTokenizer tokenPtr;
Date time1;

System.out.println("The time is: + outputFile);

code/

Can anyone tell me if I'm headed in the right direction or should I specify the length of each line read first, then design code that opens the file for reading and then create the output file for writing. This is kind of confusing as to what to do first and what steps to take and how to actually code this.
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
It's been awhile since I have done GUI development.

But you will need something along the lines of javax.swing.JFileChooser for item 2 and java.awt.FileDialog for item 3(save file).

I would also look into the sun swing tutorial

I hope this helps.

Craig
Chuck Patterson
Greenhorn

Joined: May 25, 2004
Posts: 8
Thanks for the info and steps to take to start the GUI development. We use a product called Visaj at my job for our GUI development. I will work on developing the code to read and write and do the actual conversion of the files and then work on incorporating it into the GUI, again thank you and any other suggestions would be greatly appreciated.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Well, you're off to a reasonable start. Round about the line "Long CurrTime" I see you're starting to peter out.

So, what you want to do:

1. Construct a java.io.BufferedReader, passing your FileReader as the constructor argument.

2. Write a while loop to process one line of the file at a time. here is the canonical example of how to do this.

3. For each line (perhaps in a method named "process", as in the Almanac example) use the String.split() method to break the line into tokens. See the Javadoc API for information.

4. For each token, print it to the output file in an appropriate way. Here you need to hardcode knowledge about the meaning of each field on the line.

5. Print a newline to the output file.

6. The loop body is done; go back and read another line.

If I can give you any advice as a fledgling programmer, it's this: test early and often. Use something like JUnit or even just try the program out as you write it, a bit at a time. But what you want to avoid is writing the whole thing, and battling to get it to compile, and then trying to debug it. If you write, compile, and test it a tiny step at a time, then you'll never have that awful lost feeling that comes with trying to deo everything at once.
Chuck Patterson
Greenhorn

Joined: May 25, 2004
Posts: 8
Ernest - THANK YOU, THANK YOU, THANK YOU for pointing me in the right direction because I was having a tough time trying to figure out how and where to start. Also, are you saying that I should start your suggested coding before the "Long CurrTime" line or eliminate that line along with the next two lines and do I construct the java.io.BufferedReader before or after the FileReader in/out lines?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

I don't see any use for CurrTime or what follows it. Have a look at the example from the Java Almanac and see how the BufferedReader and the FileReader relate to one another -- the FileReader has to come first.
Chuck Patterson
Greenhorn

Joined: May 25, 2004
Posts: 8
Thanks again, at least this gives me the direction that I needed. Will share the results of this development project.
Chuck Patterson
Greenhorn

Joined: May 25, 2004
Posts: 8
Me again Ernest and sorry for being a pest but I am following your suggested approach in the development the Unix time conversion utility. I have gotten to a point where I am stuck again. I have gotten to step 3 of your approach and can't seem to figure out how to us the String.split()method to break the lines into tokens. The lines I am trying to break into tokens look like:

JPM \t1080593081\t518:3:417\t496:30:147\t502:9:15\t532:9:1\t602:27:XDSVP_514\t546:27 iagnostics\t503:27:XDJ_559714\t518:20:0\t496:20:0\t
JPM \t1080593512\t518:3:418\

my code so far looks like this after reading the Javadoc API :

code
/UnixTime


import java.io.*;
import java.util.*;
import java.io.BufferedReader;
import java.io.FileReader;

public class UnixTime {

public static void main(String[] args) throws IOException {
File inputFile = new File("Unix_in.txt");
File outputFile = new File("Unix_out.txt");

FileReader in = new FileReader(inputFile);
BufferedReader br = new BufferedReader(inputFile);

try {
BufferedReader in = new BufferedReader(new FileReader("Unix_.txt")
String str;
while ((str = in.readLine()) != null) {
process(str);
}
in.close();
} catch (IOException e) {
}

String[] result = "What goes here??".split("\\s");
for (int x=0; x<result.length; x++)
System.out.println(result[x]);


PrintWriter out = new PrintWriter(outputFile);


System.out.println("The time is: + outputFile);

}
}

code/

Do I need to specify the value in each line to break it into tokens and how will I declare and define what the process method will do. Any help will be appreciated, thanks.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

This stuff (roughly) is what goes into the "process" method:



Each line, one by one, is being passed to "process" as the argument. That one line is what "process" has to worry about; and it's that argument that would replace "What goes here??"
Chuck Patterson
Greenhorn

Joined: May 25, 2004
Posts: 8
Thanks again Ernest for your help. Also, is there a representation of the Date class that will take the converted local time of the file that this utility will convert from Unix time - i.e local California time and display that time in another timezone at the time that file was originally created in for instance - South Carolina, thanks
Chuck Patterson
Greenhorn

Joined: May 25, 2004
Posts: 8
Another question - how and where do I declare the code that will actually convert the Unix time into human time. Would I do something like:

code

system.out.println(new java.util.Date(???L * 1000L)

code/

If so, then what would I put as the value since they are all different where I have the ? marks or would I use this at all, help will be greatly appreciated, thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unix Time Conversion
 
Similar Threads
IBM 157 ICE Sample Exam Answers
000-341 sample test questions
IBM Exam 157 - Questions and Answers (Sample Exam)
How to Read the Time Stamp of a File
Shell Script For Getting List Of File name In A directory