Problem to replace one date with another in a text file (with Java)?

0

I have a text file that comes as a parameter to one of my Java methods. First I go through the text file line by line in search of the Emission Date:

If you find it, I extract the date of that line in a variable. As always it is the same, I know that if you find it, I have to do a substring (84,93) to get the date inside a variable and then make a replace () for a new date!

The issue is that the replace () is not going well and does not change the date! I leave my code for you to see:

public class DateFormater {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {

           // Open the file
            FileInputStream fstream = new FileInputStream("/home/incentivate/Desktop/AC0121.TXT");
            BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
            String strLine;

            //Read File Line By Line
            while ((strLine = br.readLine()) != null)   {
                if(strLine.contains("Fecha de Emision: ")){

                    String date = strLine.substring(84,93);
                        if(date.equals("6/08/2018")){
                        System.out.println("OK");
                        strLine.replace(date, "06/08/2018"); // regex  / cosa a reemplazar
                        System.out.println(strLine);
                    }
                }
            }
            //Close the input stream
            br.close();
    }

In the second if it enters well because it prints the "OK", but when I print the line (strLine) to see if it did the replace () I see that it did not. That could be happening ?

Basically what I need is to put a 0 (zero) in front of the date that comes in the text file.

Example: If the date comes like this: 6/08/2018 I need it to look like this: 06/08/2018

    
asked by Nacho Zve De La Torre 16.10.2018 в 15:50
source

1 answer

2

You can try something different, for example instead of replacing you can give it a specific format to date:

public class DateFormater {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {

           // Open the file
            FileInputStream fstream = new FileInputStream("/home/incentivate/Desktop/AC0121.TXT");
            BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
            String strLine;

            //Read File Line By Line
            while ((strLine = br.readLine()) != null)   {
                if(strLine.contains("Fecha de Emision: ")){
//Aqui agregamos un formato a la fecha 00-00-0000
                    String date = strLine.substring(84,93);
                    SimpleDateFormat format = new SimpleDateFormat("MM-dd-YYYY");
   String dateString = format.format(new Date(date));
                }
            }
            //Close the input stream
            br.close();
    }
    
answered by 16.10.2018 / 16:11
source