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 "Issuance Date" what should come next is the date itself (EXAMPLE: 12/06/2018). I am assuming that the date is between positions 84 and 93
String date = strLine.substring(84,93);
But it's not always like this.
What is fixed is that the date always starts two spaces after the String "Issuance Date".
Ask:
How do I tell my program to do that? That is, move two characters to the right from "Issue Date" to be able to later validate the date format and other things that I have already done !!!
From already thank you very much :)
With String.indexOf() you should be able to get the position of the word you indicate, and since it returns -1 if not found, you can replace the
contains
.To get something like this:
Another way to achieve it would be with Regex , but what I indicate should help you if it always comes like this.
You search for the string, position yourself behind it and read what you need:
Try it online!