Skip to content

Commit

Permalink
Merge pull request #153 from AndrewJanong/date-bug
Browse files Browse the repository at this point in the history
  • Loading branch information
zekone authored Nov 9, 2023
2 parents 9cedc7b + a8215d9 commit 79be563
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
20 changes: 19 additions & 1 deletion src/main/java/seedu/address/commons/util/DateTimeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.format.ResolverStyle;

/**
* Utilities about date and time
Expand All @@ -19,16 +22,31 @@ public class DateTimeUtil {
*/
public static LocalDateTime parseString(String str) throws DateTimeParseException {
requireNonNull(str);
DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm[:ss]");

DateTimeFormatter formatter1 = DateTimeFormatter
.ofPattern("uuuu-MM-dd HH:mm[:ss]")
.withResolverStyle(ResolverStyle.STRICT);

DateTimeFormatter onlyDateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
DateTimeFormatter onlyTimeFormatter = DateTimeFormatter.ofPattern("HH:mm[:ss]");

LocalDateTime result = null;

try {
result = LocalDateTime.parse(str, formatter1);
} catch (DateTimeParseException e2) {
LocalDateTime now = LocalDateTime.now();

if (str.contains("-")) {
// str only contains date, will immediately throw exception otherwise
LocalDate onlyDate = LocalDate.parse(str, onlyDateFormatter);

String appendTime = " 00:00:00";
result = LocalDateTime.parse(str + appendTime, formatter1);
} else {
//str only contains time, will immediately throw exception otherwise
LocalTime onlyTime = LocalTime.parse(str, onlyTimeFormatter);

int month = now.getMonthValue();
int day = now.getDayOfMonth();
String appendDate = now.getYear() + "-" + (month <= 9 ? "0" + month : month)
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/seedu/address/logic/parser/ParserUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,12 @@ public void parseEventTime_fail_wrongFormat() {
assertThrows(ParseException.class, EventTime.MESSAGE_NON_EMPTY, () -> ParserUtil.parseEventTime(""));
String dateNow = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
assertThrows(ParseException.class,
EventTime.MESSAGE_INVALID_DATETIME_FORMAT + "Text '" + dateNow
+ " 1' could not be parsed at index 11", () ->
EventTime.MESSAGE_INVALID_DATETIME_FORMAT
+ "Text '1' could not be parsed at index 0", () ->
ParserUtil.parseEventTime("1"));
assertThrows(ParseException.class,
EventTime.MESSAGE_INVALID_DATETIME_FORMAT + "Text '" + dateNow
+ " 1,2,3,4' could not be parsed at index 11", () ->
EventTime.MESSAGE_INVALID_DATETIME_FORMAT
+ "Text '1,2,3,4' could not be parsed at index 0", () ->
ParserUtil.parseEventTime("1,2,3,4"));
}

Expand Down

0 comments on commit 79be563

Please sign in to comment.