From e6a51b16872d90e42b7f418918d0d6fa37ce7eb9 Mon Sep 17 00:00:00 2001 From: rebeccalaujx Date: Mon, 30 Aug 2021 19:52:29 +0800 Subject: [PATCH] change dates to MMM dd yyyy format --- src/main/java/Deadline.java | 15 +++++++++++++-- src/main/java/Duke.java | 4 ++-- src/main/java/Event.java | 15 +++++++++++++-- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java index 2407663ada..e09e406d08 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/Deadline.java @@ -1,10 +1,21 @@ +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; + public class Deadline extends Task { protected String by; + protected String date = ""; public Deadline(String description, String by) throws DukeException { super(description); - this.by = by; + + try { + LocalDate localDate = LocalDate.parse(by); + this.date = localDate.format(DateTimeFormatter.ofPattern("MMM dd yyyy")); + } catch (DateTimeParseException e) { + throw new DukeException("Deadline should be in a yyyy-mm-dd format."); + } if (description.isEmpty() || description == "" || description == " ") { throw new DukeException("☹ OOPS!!! The description of a deadline cannot be empty."); @@ -15,7 +26,7 @@ public Deadline(String description, String by) throws DukeException { if (by.isEmpty() || by == "" || by == " ") { throw new DukeException("☹ OOPS!!! The deadline of this task must be indicated."); } else { - this.by = by.substring(1); + this.by = this.date; } } diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 16b2b8125a..8d9bf36160 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -68,7 +68,7 @@ public static void main(String[] args) { String taskDesc = input.replaceFirst("^deadline", "").split(" /")[0]; String deadline = ""; if (input.contains("/by")) { - deadline = input.substring(input.indexOf("/by") + 3); + deadline = input.substring(input.indexOf("/by") + 4); } Deadline dTask = new Deadline(taskDesc, deadline); ls.addTask(dTask); @@ -77,7 +77,7 @@ public static void main(String[] args) { String taskDesc = input.replaceFirst("^event", "").split(" /")[0]; String eventTime = ""; if (input.contains("/at")) { - eventTime = input.substring(input.indexOf("/at") + 3); + eventTime = input.substring(input.indexOf("/at") + 4); } Event eTask = new Event(taskDesc, eventTime); ls.addTask(eTask); diff --git a/src/main/java/Event.java b/src/main/java/Event.java index b6a79ad165..128dec9d1a 100644 --- a/src/main/java/Event.java +++ b/src/main/java/Event.java @@ -1,10 +1,21 @@ +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; + public class Event extends Task { protected String at; + protected String date = ""; public Event(String description, String at) throws DukeException { super(description); - this.at = at; + + try { + LocalDate localDate = LocalDate.parse(at); + this.date = localDate.format(DateTimeFormatter.ofPattern("MMM dd yyyy")); + } catch (DateTimeParseException e) { + throw new DukeException("Event date should be in a yyyy-mm-dd format."); + } if (description.isEmpty() || description == "" || description == " ") { throw new DukeException("☹ OOPS!!! The description of an event cannot be empty."); @@ -15,7 +26,7 @@ public Event(String description, String at) throws DukeException { if (at.isEmpty() || at == "" || at == " ") { throw new DukeException("☹ OOPS!!! The time of the event must be indicated."); } else { - this.at = at.substring(1); + this.at = this.date; } }