package seedu.taskitty.model.task;

import seedu.taskitty.commons.exceptions.IllegalValueException;

/**
 * Represents a Task's date in the task manager.
 * Guarantees: immutable; is valid as declared in {@link #isValidDate(String)}
 */
public class TaskDate {

    public static final String MESSAGE_DATE_CONSTRAINTS =
            "Task dates should be in the format dd/mm/yyyy or ddmmyyyy or dd monthname yyyy";
    public static final String MESSAGE_DATE_INVALID =
            "Task date provided is invalid!";
    
    //format: dd/mm/yyyy
    private static final String DATE_VALIDATION_REGEX_FORMAT_1 =
            "[\\p{Digit}]{1,2}/[\\p{Digit}]{1,2}/[\\p{Digit}]{4}";
    //format: ddmmyyyy
    private static final String DATE_VALIDATION_REGEX_FORMAT_2 =
            "[\\p{Digit}]{8}";
    //format: dd monthname yyyy
    //Shortest allowed monthname is 3 characters, longest monthname is September (9 characters)
    private static final String DATE_VALIDATION_REGEX_FORMAT_3 =
            "[\\p{Digit}]{1,2}[ ]?[\\p{Alpha}]{3,9}[ ]?[\\p{Digit}]{4}";
    public static final String[] DATE_VALIDATION_REGEXES = {
            DATE_VALIDATION_REGEX_FORMAT_1,
            DATE_VALIDATION_REGEX_FORMAT_2,
            DATE_VALIDATION_REGEX_FORMAT_3 };

    public final String date;

    /**
     * Validates given name.
     *
     * @throws IllegalValueException if given name string is invalid.
     */
    public TaskDate(String date) throws IllegalValueException {
        assert date != null;
        date = date.trim();
        if (!isValidDate(date)) {
            throw new IllegalValueException(MESSAGE_DATE_CONSTRAINTS);
        }
        this.date = date;
    }

    /**
     * Returns true if a given string is a valid person name.
     */
    public static boolean isValidDate(String test) {
        for (String format : DATE_VALIDATION_REGEXES) {
            if (test.matches(format)) {
                return true;
            }
        }
        return false;
    }


    @Override
    public String toString() {
        return date;
    }

    @Override
    public boolean equals(Object other) {
        return other == this // short circuit if same object
                || (other instanceof TaskDate // instanceof handles nulls
                && this.date.equals(((TaskDate) other).date)); // state check
    }

    @Override
    public int hashCode() {
        return date.hashCode();
    }

}

@@ -29,16 +29,124 @@
 .task;%0A%0A
+import java.time.DateTimeException;%0Aimport java.time.LocalDate;%0Aimport java.time.format.DateTimeFormatter;%0A%0A
 import s
@@ -200,16 +200,61 @@
 ception;
+%0Aimport seedu.taskitty.commons.util.DateUtil;
 %0A%0A/**%0A *
@@ -660,16 +660,83 @@
 alid!%22;%0A
+    public static final String DATE_DISPLAY_FORMAT = %22dd/MM/yyyy%22;%0A
     %0A   
@@ -1487,22 +1487,25 @@
 c final 
-String
+LocalDate
  date;%0A%0A
@@ -1870,16 +1870,43 @@
         
+%0A        try %7B%0A            
 this.dat
@@ -1909,22 +1909,175 @@
 .date = 
+DateUtil.parseDate(
 date
+)
 ;%0A
+        %7D catch (DateTimeException dateTimeException) %7B%0A            throw new IllegalValueException(MESSAGE_DATE_INVALID);%0A        %7D%0A
     %7D%0A%0A 
@@ -2426,24 +2426,24 @@
 oString() %7B%0A
-
         retu
@@ -2449,16 +2449,73 @@
 urn date
+.format(DateTimeFormatter.ofPattern(DATE_DISPLAY_FORMAT))
 ;%0A    %7D%0A

Improve validation