forked from AY2425S1-CS2113-W14-4/tp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request AY2425S1-CS2113-W14-4#102 from philip1304/datetime…
…-class DateTime // Implemented
- Loading branch information
Showing
1 changed file
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |