MG97 MG97 - 1 year ago 61
Java Question

Getting timestamps in Java

(Hello world level automation tester)

So I have currently got a bunch of test cases for download, this is how it goes.

Login > Navigate > Select downloads > Download > Rename the file on the computer > Open it

Then I have a small bit of java code on another class file to clean these files up after I inspect them.

The issue for me here is that I rely on a timestamp to find rename and open the file prior to renaming, after coming in this morning, I realised the timestamp difference is like 5 seconds off, though it varies, so how could I grab a timestamp, and give it a range of 10 seconds +- so that it can find it even if it's a little bit off?

Right now I've got an if else statement going, where if I get the correct timestamp it renames, else it takes -1 from the timestamp (-1 second) and finds it that way), but of course this does not work if it's more than that....

String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss").format(Calendar.getInstance().getTime());;
File fileName = new File("C:/Users/User/Downloads/" + timeStamp + (".csv"));
File newName = new File("C:/Users/User/Downloads/File.csv");
if (fileName.renameTo(newName)) {
System.out.println("File was renamed");
long timeStampLong = Long.parseLong(timeStamp);
long completeStamp = timeStampLong - 1;
String completeString = Long.toString(completeStamp);
File fileNameNew = new File("C:/Users/User/Downloads/" + completeString + (".csv"));
System.out.println("File was renamed after conversion");

I also wanted to ask whether I can still add the timestamp as part of the directory if it's an integer/long and not a string?

Also, I'm not sure what way it is better to do what I want, as right now I'm not sure how I could go about catching an error such as the file not found in directory etc. as I can't add anything after if else, or can I?

Please let me know if you need any clarification! :)

String timeStampString = new SimpleDateFormat("yyyyMMddHHmmss").format(Calendar.getInstance().getTime());
Long timeStampLong = Long.getLong(timeStampString);
String newName = "allPi";
File[] files = getMatchingFiles(fileName, timeStampLong, 50000);
for (File file:files) {
file.renameTo(new File(universal + "/Downloads/" + newName));

Kind Regards,


Answer Source

From what I understand , you are downloading a file at downloads a csv file at the target location.Then you need to read that file and rename it.

I would suggest you go about it from the "last modified" way.Therefore , you can try getting all the files written for a range of time and renaming them.

Try out the following :

public static File[] getMatchingFiles(String workingDirectory,final long timestamp,final long threshold){
    File directoryWithFiles= new File(workingDirectory);

    return directoryWithFiles.listFiles(new FilenameFilter() { 
        public boolean accept(File dir, String filename){  
            File file=new File(dir+File.separator+filename);
                if(file.lastModified()<=timestamp  && file.lastModified() >(timestamp-threshold)){
                    return true;
            return false;
    } );

workingDirectory : the download location.
timestamp : the time of download start.
threshold : the average time of download with some offset added.

Then this snippet will get you all the files which were created in the given range.Tweak your sleep range such that this range will always get you a single file.

Use it in the following manner :

File[] files=getMatchingFiles("dir",100000000,50000);

for(File file:files){
    file.renameTo(new File("<renamed File path>"))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download