Skip to content

Commit

Permalink
Cache Dates
Browse files Browse the repository at this point in the history
  • Loading branch information
PieTw3lve committed Mar 8, 2024
1 parent 85838d6 commit ca4d293
Showing 1 changed file with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@
import org.bukkit.plugin.java.JavaPlugin;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoUnit;

public class DateCalculatorPlugin extends JavaPlugin
{

private LocalDate today;
private long lastUpdated;

public void onEnable()
{
if (Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) {
Expand All @@ -21,13 +26,22 @@ public void onEnable()
}

public String getDaysBetweenToday(String targetDate) {
long now = System.currentTimeMillis();
if (today == null || now - lastUpdated >= 86400000) {
today = LocalDate.now();
lastUpdated = now;
}

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
try {
LocalDate today = LocalDate.now();
LocalDate target = LocalDate.parse(targetDate);
long days = ChronoUnit.DAYS.between(target, today);
return String.valueOf(days);
} catch (Exception e) {
LocalDate target = LocalDate.parse(targetDate, formatter);
if (target != null) {
long days = ChronoUnit.DAYS.between(target, today);
return String.valueOf(days);
}
} catch (DateTimeParseException e) {
return "Invalid date format!";
}
return "Invalid date format!";
}
}

0 comments on commit ca4d293

Please sign in to comment.