How can I move X characters to the right from a String in Java?

1

The following program reads a txt file line by line:

BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(origin),"ISO-8859-1"));

                String strLine;
                while ((strLine = br.readLine()) != null)   {
                    if(strLine.contains("Fecha de Emision: ")){
                        String date = strLine.substring(84,93);
                        String[] parts = date.split("/");
                        try{
                            int day = Integer.parseInt(parts[0]);
                            if(day < 10){
                                SimpleDateFormat format = new SimpleDateFormat("MM/dd/YYYY");
                                String dateString = format.format(new Date(date));
                                String newDate = strLine.replace(date, dateString);
                                writer.write(newDate+"\n");
                            } else{
                            writer.write(strLine+"\n");
                            }
                        } catch(NumberFormatException nfe){
                            System.out.println("Problema al parsear el día de la fecha");
                            return;
                        }
                    } else{
                        writer.write(strLine+"\n");
                      }
                }
                br.close();

If in the line you are reading you find the String "Date of Issuance" what should come next is the date itself (EXAMPLE: 06/12/2018). I'm assuming the date is between positions 84 and 93

String date = strLine.substring (84,93);

but it's not always like that.

What is fixed is that the date always starts two spaces after the String "Issue Date".

Question:

How do I tell my program to do that? In other words, move two characters to the right from "Date of Issuance" to be able to validate the date format and other things that I have already done !!!

Thank you very much:)

    
asked by Nacho Zve De La Torre 19.10.2018 в 14:57
source

2 answers

2

With String.indexOf () you should be able to get the position of the word you indicate, and as it returns -1 if it does not find, you can replace the contains .
To look something like this:

String toFind = "Fecha de Emision: ";
int position = strLine.indexOf(toFind);
if(position >= 0){
    //para cortar el Date a la posición se le suma el largo del string +1, hasta la posición + el largo del string +1 +10 que es el largo de la fecha.
    String date = strLine.substring(position+toFind.length()+1, position+toFind.length()+11);
    String[] parts = date.split("/");
    try{
        int day = Integer.parseInt(parts[0]);
        System.out.println("day "+day);
        if(day < 10){
            SimpleDateFormat format = new SimpleDateFormat("MM/dd/YYYY");
            String dateString = format.format(new Date(date));
            String newDate = strLine.replace(date, dateString);
            writer.write(newDate+"\n");
        } else{
            writer.write(strLine+"\n");
        }
    } catch(NumberFormatException nfe){
        System.out.println("Problema al parsear el día de la fecha");
        return;
    }
} else{
    writer.write(strLine+"\n");
}

Another way to achieve this would be with Regex , but what I told you should serve you if it always comes like this.

    
answered by 19.10.2018 / 15:55
source
1

You search for the chain, you position yourself behind it and read what you need:

public class Main {
    static private String LABEL = "Fecha de emisión: ";
    static private int DATE_LENGTH = 10; // DD/MM/YYYY

    public static void main(String[] args) {
        String s = "En un lugar de la Mancha.... Fecha de emisión: 22/11/2018";

        System.out.println(s.indexOf(LABEL)); // 29

        int index = s.indexOf(LABEL) + LABEL.length();
        System.out.println(s.substring(index, index + DATE_LENGTH));
    }
}

online Try it!

    
answered by 19.10.2018 в 15:54