import java.io.*; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; import java.util.ArrayList; public class Birthdays { public static void main(String[] args) throws IOException { File file = new File("C:\\Users\\David\\TechnikNews Cloud\\1 Schule\\0 HTL 201920\\POSE\\HUE_03_BirthdaysFile\\geburtstage.txt"); ArrayList geburtstage = parseData(file); System.out.println("Als nächstes Geburtstag hat: "); System.out.println(calculateNextBirtday(geburtstage)); } public static ArrayList parseData(File file) throws IOException { BufferedReader br = new BufferedReader(new FileReader(file)); String read; String content[]; ArrayList geburtstage = new ArrayList(); while ((read = br.readLine()) != null) { if (!read.startsWith("#")) { content = read.split(","); if (content.length == 4) { Geburtstag g = new Geburtstag(content[0], Integer.parseInt(content[1]), Integer.parseInt(content[2]), Integer.parseInt(content[3])); geburtstage.add(g); } else { System.out.println("Fehler, diese Zeile kann nicht geparst werden: zu viele oder zu wenig Daten (" + read + ")"); } } else { // do nothing, don't care about this line } } return geburtstage; } public static String calculateNextBirtday(ArrayList geburtstage) { DateTimeFormatter germanDate = DateTimeFormatter.ofPattern("dd.MM.YYYY"); LocalDate today = LocalDate.now(); long diff = ChronoUnit.DAYS.between(today, geburtstage.get(0).naechsteGeburtstagsfeier()); int pos = 0; for (int i = 0; i < geburtstage.size(); i++) { if ((ChronoUnit.DAYS.between(today, geburtstage.get(i).naechsteGeburtstagsfeier())) < diff) { diff = ChronoUnit.DAYS.between(today, geburtstage.get(i).naechsteGeburtstagsfeier()); pos = i; } } String output = "Name: " + geburtstage.get(pos).name + " | nächster Geburtstag: " + geburtstage.get(pos).naechsteGeburtstagsfeier().format(germanDate); return output; } }