Generate a txt file, from another given, with the dates ordered, and those between two other dates given

Iván Martín Jiménez :

I am trying to make a method that, given an input txt file and two LocalDate dates, return another file with the dates between the two given, and sorted. I already know how to read the file, create another one, and introduce in the new file the dates sorted. But I have no idea about how to make the dates between the two given. I am trying to do it with while loops. If you have some idea, it would be amazing to light me the way. I show you the code that already have, thank you.

private static File fileGenerator(String f_input, LocalDate Date1, LocalDate date2) throws FileNotFoundException, IOException {
 FileReader fr=new FileReader (f_input);
 BufferedReader br=new BufferedReader(fr);
 File f_output=new File("C:/Users/Ivan/Documents/output_file.txt");
 FileWriter fw = new FileWriter(f_ouput);
 BufferedWriter bw = new BufferedWriter(fw);

 //Here, i create a list where I will drop every line from f_input
 LinkedList<String> list = new LinkedList<String>();
 String line=null;
 while((line=br.readLine())!=null) {
    list.add(line);
 {

 //Now, I sort the list
 Collections.sort(list);

 Iterator iter = list.iterator();
 String c;
 while(iter.hasNext()){
    c=(String) iter.next();
    bw.append(c);
    bw.newLine();
    bw.flush();
 {

 br.close();
 fr.close();
 fw.close();

 return f_ouput;
Nicholas K :

You need to perform the following: (psuedocode if you like)

  1. Convert each string from the file using the following code :

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
    // for example 
    LocalDate test = LocalDate.parse(string_frm_file, formatter);
    
  2. Now you need to compare that LocalDate (here, test) to see if it falls within your range passed in to your method. If it does add it to the list to be written to the file, else ignore it.

    if (date1.isBefore(test) && date2.isAfter(test)) {
        list.add(test);
    }
    

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=131414&siteId=1