0

I have 5 files.

1.ICAREP_ANI_SVCPROF_20120614_001.DAT
2.ICAREP_ANI_SVCPROF_20120617_001.DAT
3.ICAREP_ANI_SVCPROF_20120619_001.DAT
4.ICAREP_ANI_SVCPROF_20120615_001.DAT
5.ICAREP_ANI_SVCPROF_20120616_001.DAT

I want to get date (ex."20120614") from that filenames and compare each date to find which is the latest one using java programming.

Anyone can help me on this?

EDIT! Below is my code:

 String[] children = dir.list();
 String test = "";
 for (i=0; i<children.length; i++) {
   test = children[i].compareTo(children[i+1]);
 }
12
  • What have you attempted? Commented Dec 28, 2012 at 6:38
  • If your file names always contains this letters, than you can simple compare filenames to get the latest one. Commented Dec 28, 2012 at 6:38
  • Squiguy - I don't know how to get the date. So that how can I start? Commented Dec 28, 2012 at 6:43
  • Yatul - what do you means by simple compare filenames?I want to compare the date. Commented Dec 28, 2012 at 6:44
  • 1
    What have You tried? Commented Dec 28, 2012 at 6:44

6 Answers 6

1

here is the simple logic you can use,

String filepath = "ICAREP_ANI_SVCPROF_20120614_001.DAT";
        String [] tempPath = filepath.split("_");
        System.out.println(""+tempPath[3]); //output = 20120614

        SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd");
        try
        {
            Date date1 = ft.parse(tempPath[3]); //covert the string into date
        }
        catch(ParseException ex)
        {
            System.out.println(""+ex);
        }

        //compare the date logic

from the code below you can get the date on the file by using split ("_") , so the length of the file name doesn't matter,

after that you can parse the string using the SimpleDateFormat with the "yyMMdd" pattern.

and then use the ft.parse(string) to generate the Date object.

Sign up to request clarification or add additional context in comments.

1 Comment

String comparison on file names will give the same result but more efficiently.
1
int numbDate = Integer.parseInt((String) str.subSequence(19, 27))

where str is your filename.

Comments

0

If your file names will look the same always you don't need to extract the date, you can simply use string comparison for file names:

file1.getName().compareTo(file2.getName())

This way you can find the latest.

4 Comments

I cannot use the function of compareTo. Any ideas?
I am not sure. It turn error " incompatible types found : int required: java.lang.String "
I just edited my code above. Could you please check what is the problem?
The problem is that String.compareTo() returns an integer depending on which its argument is smaller and you try to assign this integer to test which is String. What you need to do is set test = children[0] and then use the for loop to iterate on the array and compare the elements to test, e.g. 'test.compareTo(children[i]), and if the result is positive, then assign the children[i] value to test, because it is greater (lexicographically). After the loop, test will contain the greatest element (which is the one with the latest date)
0

Here's a one liner:

Date date = new SimpleDateFormat("yyyyMMdd").parse(filename.replaceAll(".*(\\d{8}).*", "$1"));

Comments

0
enter code here


String[] ar = { "ICAREP_ANI_SVCPROF_20120614_001.DAT", "ICAREP_ANI_SVCPROF_20120617_001.DAT",
        "ICAREP_ANI_SVCPROF_20120619_001.DAT", "ICAREP_ANI_SVCPROF_20120615_001.DAT",
        "ICAREP_ANI_SVCPROF_20120616_001.DAT" };

int latestIndex = 0;
int latest = 0;
String latestFile = ar[0];
for (int i = 0; i < ar.length; i++) {
    String tempString = ar[i].substring(0, ar[i].lastIndexOf('_'));
    latestFile = tempString.substring(tempString.lastIndexOf('_') + 1);
    int current = Integer.parseInt(latestFile);
    if (latest < current) {
    latestIndex = i;
    latest = current;
    }
}

System.out.println("Latest file: " + ar[latestIndex]);

Comments

-1

If they are in a particular folder you can first get the file name and parse it accordingly to get a particular string. Try this one

`

       public void listFilesForFolder(final File folder) {
            for (final File fileEntry : folder.listFiles()) {
                if (fileEntry.isDirectory()) {
                    listFilesForFolder(fileEntry);
                } else {
                    if(fileEntry.getName().contains("20120614"){
//code to compare the latest date
}

                }
        }
}


Try this out, I guess this is what you wanted...

2 Comments

@downvoter... Please comment the reason. If I made any mistake. It will help me in further learning the things.
Your answer fails to address the Question. The Question is about comparing strings which contain date-time values, not navigating the hierarchy of a file system as you do in your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.