import java.io.Serializable; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; public class TimeStamp implements Comparable, Serializable { private static final long serialVersionUID = -2419868536330628838L; private final LocalDate datum; public TimeStamp(int jaar, int maand, int dagVanDeMaand) { this(LocalDate.of(jaar, maand, dagVanDeMaand)); } public TimeStamp(LocalDate datum) { this.datum = datum; } public LocalDate getDatum() { return datum; } public int getJaar() { return datum.getYear(); } public int getMaand() { return datum.getMonthValue(); } public int getDagVanDeMaand() { return datum.getDayOfMonth(); } public boolean isBefore(TimeStamp other) { return this.datum.isBefore(other.datum); } public boolean isAfter(TimeStamp other) { return this.datum.isAfter(other.datum); } /** * Berekent absolute verschil tussen de meegegeven timestamps. Geeft dus nooit een negatieve waarde terug. */ public static int getDaysBetween(TimeStamp t1, TimeStamp t2) { return Math.abs((int) t1.datum.until(t2.datum, ChronoUnit.DAYS)); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((datum == null) ? 0 : datum.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; TimeStamp other = (TimeStamp) obj; if (datum == null) { if (other.datum != null) return false; } else if (!datum.equals(other.datum)) return false; return true; } @Override public int compareTo(TimeStamp other) { return this.datum.compareTo(other.datum); } private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("dd/MM/yyyy"); @Override public String toString() { return datum.format(FORMATTER); } public static TimeStamp parse(String datumString) { return new TimeStamp(LocalDate.parse(datumString, FORMATTER)); } }