package seedu.task.model.util;

import java.util.Calendar;

import seedu.task.commons.exceptions.IllegalValueException;

public class DateParser {
    public static final String DATE_STRING_ILLEGAL_FORMAT =
            "String given to the DateParser must be of the form YYYY/MM/DD HHMM";

    private static final int DEFAULT_SECONDS = 0;
    private static final int DEFAULT_MILLISECONDS = 0;
    private static final String DATE_STRING_VALIDATION_REGEX = "[0-9]{4}/[0-1][0-9]/[0-3][0-9] [0-2][0-9][0-5][0-9]";
    private static final String EMPTY_DATE_STRING = "";
    private static final int MONTH_OFFSET = 1;

    public static Calendar parse(String date) throws IllegalValueException {
        if (!isValidDateString(date)) {
            throw new IllegalValueException(DATE_STRING_ILLEGAL_FORMAT);
        }
        Calendar cal = Calendar.getInstance();
        int year = getYear(date);
        int month = getMonth(date);
        int day = getDay(date);
        int hour = getHour(date);
        int minute = getMinute(date);
        cal.set(year, month, day, hour, minute, DEFAULT_SECONDS);
        cal.set(Calendar.MILLISECOND, DEFAULT_MILLISECONDS);
        return cal;
    }

    public static String toString(Calendar date) {
        String dateString;

        if (date == null) {
            return EMPTY_DATE_STRING;
        }

        int year = date.get(Calendar.YEAR);
        int month = date.get(Calendar.MONTH);
        int day = date.get(Calendar.DATE);
        int hour = date.get(Calendar.HOUR_OF_DAY);
        int minute = date.get(Calendar.MINUTE);

        dateString = String.format("%4d/%02d/%02d %02d%02d", year, month + MONTH_OFFSET, day, hour, minute);

        return dateString;
    }

    public static boolean isValidDateString(String test) {
        return test.matches(DATE_STRING_VALIDATION_REGEX);
    }

    private static int getYear(String date) {
        return Integer.parseInt(date.substring(0,  4));
    }

    private static int getMonth(String date) {
        return Integer.parseInt(date.substring(5,  7)) - MONTH_OFFSET;
    }

    private static int getDay(String date) {
        return Integer.parseInt(date.substring(8, 10));
    }

    private static int getHour(String date) {
        return Integer.parseInt(date.substring(11, 13));
    }

    private static int getMinute(String date) {
        return Integer.parseInt(date.substring(13));
    }
}

@@ -1,8 +1,32 @@
+//@@evanyeung A0163744B%0A
 package 
@@ -248,32 +248,8 @@
 ing 
-given to the DateParser 
 must
@@ -281,16 +281,98 @@
 D HHMM%22;
+%0A    public static final String DATE_ILLEGAL_DATE = %22The given date is not valid%22;
 %0A%0A    pr
@@ -1105,32 +1105,165 @@
 etMinute(date);%0A
+        if(!isValidDate(year, month, day, hour, minute)) %7B%0A            throw new IllegalValueException(DATE_ILLEGAL_DATE);%0A        %7D%0A
         cal.set(
@@ -1394,24 +1394,42 @@
 n cal;%0A    %7D
+%0A    //@@evanyeung
 %0A%0A    public
@@ -1947,24 +1947,52 @@
 ing;%0A    %7D%0A%0A
+    //@@evanyeung A0163744B%0A
     public s
@@ -2034,24 +2034,24 @@
 ing test) %7B%0A
-
         retu
@@ -2096,32 +2096,579 @@
 _REGEX);%0A    %7D%0A%0A
+    private static boolean isValidDate(int year, int month, int day, int hour, int minute) %7B%0A        if (year %3C 0 %7C%7C month %3C 0 %7C%7C month %3E 11) %7B%0A            return false;%0A        %7D%0A        Calendar cal = Calendar.getInstance();%0A        cal.set(Calendar.YEAR, year);%0A        cal.set(Calendar.MONTH, month);%0A        if (day %3C 1 %7C%7C day %3E cal.getActualMaximum(Calendar.DAY_OF_MONTH)) %7B%0A            return false;%0A        %7D%0A        if (hour %3C 0 %7C%7C hour %3E 23 %7C%7C minute %3C 0 %7C%7C minute %3E 59) %7B%0A            return false;%0A        %7D%0A        return true;%0A    %7D%0A%0A
     private stat

Add date value validation to DateParser