0

I have two java programs

1) First program runs everyday and generates file abc_currenttimestamp.csv (e.g. abc_1357615107)

2) Second program runs everyday and reads file generated by first program. But the file it has to read is previous days file and not the file that was generated today.

For e.g first program generates files on 9th Jan and 10th Jan etc. Second program starts running from 10th and should read file having time-stamp of 9th Jan.

One way could be second program checks the time-stamp of all files and finds if it is previous days time-stamp. I don't know how to do this in Java.

Another way could be that first program writes the file name to a field and second program knows file name by reading from this field. But the problem is every time first program runs everyday its going overwrite this field with today's file name.

Thanks!

SUM
  • 1,651
  • 4
  • 33
  • 57
  • 1
    Both of your ideas should work. Pick one and if you face a problem while implementing it, feel free to ask again. – MrSmith42 Jan 09 '13 at 18:27
  • Thanks. Second idea would be easy for me to implement. But it has the problem of field value being overwritten with today's file time-stamp whereas second program is expecting the field value to have yesterday's time-stamp. – SUM Jan 09 '13 at 18:31
  • 1
    use two fields to store the last two timestamps – MrSmith42 Jan 09 '13 at 18:34
  • I did not follow how having two fields to store would help – SUM Jan 09 '13 at 18:36

2 Answers2

0

You can simply play with

file.lastModified();

or you just add date and time in file name itself like ddmmyyyy.csv then you can easily check whether it exists or not.

vels4j
  • 11,208
  • 5
  • 38
  • 63
0

I like the idea of getting the timestamp from the file names. That way, someone modifying the file by hand doesn't mess up your process.

It sort of depends on what kind of timestamp value you have. If its java use the code below and if its unix multiply by 1000 and then do this.

Calendar today = Calendar.getInstance(); // use this to get todays date (and time)

Calendar mydate = Calendar.getInstance(); // this uses the timestamp for comparing
mydate.setTimeInMillis(timestamp);

// This prints out the date and shows how to get values to compare. You can also 
//  subtract one day from today's date to handle rollover of months and years.
out.println( mydate.get(Calendar.DAY_OF_MONTH)+"."+mydate.get(Calendar.MONTH)+"."+mydate.get(Calendar.YEAR));

There is some discussion from here:

Java: Date from unix timestamp

Community
  • 1
  • 1
Lee Meador
  • 12,829
  • 2
  • 36
  • 42