Skip to content

Commit

Permalink
Merge pull request AY2425S1-CS2113-W14-4#102 from philip1304/datetime…
Browse files Browse the repository at this point in the history
…-class

DateTime // Implemented
  • Loading branch information
philip1304 authored Oct 20, 2024
2 parents ef78c92 + b8b15e1 commit 4817675
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/main/java/seedu/pill/util/DateTime.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package seedu.pill.util;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

public class DateTime implements Comparable<DateTime> {
private LocalDateTime dateTime;

public DateTime() {
this.dateTime = LocalDateTime.now();
}

public DateTime(LocalDateTime dateTime) {
this.dateTime = dateTime;
}

public LocalDateTime getDateTime() {
return dateTime;
}

public void setDateTime(LocalDateTime dateTime) {
this.dateTime = dateTime;
}

public String getFormattedDateTime(String format) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
return dateTime.format(formatter);
}

public String getFormattedDate() {
return getFormattedDateTime("yyyy-MM-dd");
}

public String getFormattedTime() {
return getFormattedDateTime("HH:mm:ss");
}

@Override
public int compareTo(DateTime other) {
return this.dateTime.compareTo(other.getDateTime());
}

public boolean isAfter(DateTime other) {
return this.compareTo(other) > 0;
}

public boolean isBefore(DateTime other) {
return this.compareTo(other) < 0;
}

public long getDaysUntil(DateTime other) {
return ChronoUnit.DAYS.between(this.dateTime, other.getDateTime());
}

public boolean isExpired(DateTime expirationDate) {
return this.isAfter(expirationDate);
}

public long getDaysUntilExpiration(DateTime expirationDate) {
return getDaysUntil(expirationDate);
}

public boolean isWithinRefillPeriod(int daysBeforeRefill) {
LocalDateTime refillDate = this.dateTime.plusDays(daysBeforeRefill);
return LocalDateTime.now().isAfter(refillDate);
}

@Override
public String toString() {
return getFormattedDateTime("yyyy-MM-dd HH:mm:ss");
}
}

0 comments on commit 4817675

Please sign in to comment.