|
| 1 | +/* Licensed under MIT 2026. */ |
| 2 | +package ui; |
| 3 | + |
| 4 | +import lombok.NonNull; |
| 5 | + |
| 6 | +import java.time.LocalDateTime; |
| 7 | +import java.util.regex.Matcher; |
| 8 | +import java.util.regex.Pattern; |
| 9 | + |
| 10 | +/** |
| 11 | + * A date represents any day by its year, month of the year (1-12) and day (1-31 |
| 12 | + * usually). Dates are immutable and compared by values, Dates have multiple |
| 13 | + * methods for earlier/later comparison. |
| 14 | + * |
| 15 | + * @param year The year. |
| 16 | + * @param month The month (1-12). |
| 17 | + * @param day The day of the month (1-31). |
| 18 | + */ |
| 19 | +public record Date(int year, int month, int day) { |
| 20 | + |
| 21 | + private static final Pattern DATE_PATTERN = Pattern.compile("^(\\d{1,7})-(\\d{1,2})-(\\d{1,2})$"); |
| 22 | + |
| 23 | + public static final Date ZERO_DATE = new Date(0, 0, 0); |
| 24 | + |
| 25 | + @Override |
| 26 | + @NonNull |
| 27 | + public String toString() { |
| 28 | + return "%04d-%02d-%02d".formatted(year, month, day); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * If the Date is zero, so all fields are zero, and thus equal to the constant |
| 33 | + * {@link Date#ZERO_DATE}. |
| 34 | + * |
| 35 | + * @return True if the Date is the same as ZERO_DATE, false if not. |
| 36 | + */ |
| 37 | + public boolean isZero() { |
| 38 | + return this.equals(ZERO_DATE); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Returns if this Date is earlier than another given Date. If the two Dates are |
| 43 | + * equal, returns false. |
| 44 | + * <p> |
| 45 | + * If the given Date is null, returns false. |
| 46 | + * </p> |
| 47 | + * |
| 48 | + * @return True if the other Date is earlier, false if not. |
| 49 | + */ |
| 50 | + public boolean isEarlierThan(Date other) { |
| 51 | + if (other == null) |
| 52 | + return false; |
| 53 | + if (other.year() > this.year()) |
| 54 | + return true; |
| 55 | + if (other.year() < this.year()) |
| 56 | + return false; |
| 57 | + if (other.month() > this.month()) |
| 58 | + return true; |
| 59 | + if (other.month() < this.month()) |
| 60 | + return false; |
| 61 | + return other.day() > this.day(); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Returns if this Date is earlier than or equal to another given Date. If the |
| 66 | + * two Dates are equal, returns true. |
| 67 | + * <p> |
| 68 | + * If the given Date is null, returns false. |
| 69 | + * </p> |
| 70 | + * |
| 71 | + * @return True if the other Date is earlier or equal, false if not. |
| 72 | + */ |
| 73 | + public boolean isEarlierThanOrEqual(Date other) { |
| 74 | + if (other == null) |
| 75 | + return false; |
| 76 | + if (this.equals(other)) |
| 77 | + return true; |
| 78 | + return isEarlierThan(other); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Returns if this Date is later than another given Date. If the two Dates are |
| 83 | + * equal, returns false. |
| 84 | + * <p> |
| 85 | + * If the given Date is null, returns false. |
| 86 | + * </p> |
| 87 | + * |
| 88 | + * @return True if the other Date is later, false if not. |
| 89 | + */ |
| 90 | + public boolean isLaterThan(Date other) { |
| 91 | + if (other == null || this.equals(other)) |
| 92 | + return false; |
| 93 | + return !isEarlierThan(other); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Returns if this Date is later than or equal to another given Date. If the two |
| 98 | + * Dates are equal, returns true. |
| 99 | + * <p> |
| 100 | + * If the given Date is null, returns false. |
| 101 | + * </p> |
| 102 | + * |
| 103 | + * @return True if the other Date is later or equal, false if not. |
| 104 | + */ |
| 105 | + public boolean isLaterThanOrEqual(Date other) { |
| 106 | + if (other == null) |
| 107 | + return false; |
| 108 | + if (this.equals(other)) |
| 109 | + return true; |
| 110 | + return !isEarlierThan(other); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Parses a Date from the format YYYY-MM-DD. This format is required but allows |
| 115 | + * some variance in the length of numbers. <br/> |
| 116 | + * Restrictions for year, month and day: |
| 117 | + * <ul> |
| 118 | + * <li>Year: A number consisting of 1-7 digits.</li> |
| 119 | + * <li>Month: A number consisting of 1-2 digits.</li> |
| 120 | + * <li>Day: A number consisting of 1-2 digits.</li> |
| 121 | + * </ul> |
| 122 | + * |
| 123 | + * <p> |
| 124 | + * If not Date can be parsed, returns the ZERO_DATE, so the default Date where |
| 125 | + * all fields are 0. |
| 126 | + * </p> |
| 127 | + * |
| 128 | + * @param date The date, format YYYY-MM-DD. |
| 129 | + * @return The parsed date. |
| 130 | + */ |
| 131 | + public static Date parseDate(String date) { |
| 132 | + Matcher matcher = DATE_PATTERN.matcher(date); |
| 133 | + if (!matcher.matches()) |
| 134 | + return ZERO_DATE; |
| 135 | + int year = Integer.parseInt(matcher.group(1)); |
| 136 | + int month = Integer.parseInt(matcher.group(2)); |
| 137 | + int day = Integer.parseInt(matcher.group(3)); |
| 138 | + return new Date(year, month, day); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Gets the current local Date set on this computer. |
| 143 | + * |
| 144 | + * @return The current local date. |
| 145 | + */ |
| 146 | + public static Date currentLocalDate() { |
| 147 | + LocalDateTime localDateTime = LocalDateTime.now(); |
| 148 | + return new Date(localDateTime.getYear(), localDateTime.getMonthValue(), localDateTime.getDayOfMonth()); |
| 149 | + } |
| 150 | +} |
0 commit comments