Finding the diffrence in time

Discussion in 'Spigot Plugin Development' started by MrSpyMan, Apr 30, 2015.

  1. I am trying to make a plot plugin with a rent and if you do not pay your rent in 30 days then your plot will be cleared. I am not sure how to compare the dates of when the plot was rented and when the plot will expire. I want to try to find the difference of it.

    Also don't recommend using a Runnable.. It's unreasonable to expect my server to be constantly up for a month.

    This is what I have so far..
    Code (Text):
    //When you rent a plot it will store this in a config file.
            Date now = new Date();
            SimpleDateFormat format = new SimpleDateFormat(
                    "yyyy-MM-dd HH:mm:ss");
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.MONTH, 1);
            getConfig().set("Plots.PlotID." + plot_id + "OrginalRent", format.format(now));
            getConfig().set("Plots.PlotID." + plot_id + "PlotExpire", format.format(cal.getTime()));
           
    Code (Text):
    //Checking if the plot will expire
            SimpleDateFormat format = new SimpleDateFormat(
                    "yyyy-MM-dd HH:mm:ss");
            Date orgrent = format.parse(getConfig().getString("Plots.PlotID." + plot_id + "OrginalRent"));
            Date expiretime = format.parse("Plots.PlotID." + plot_id + "PlotExpire");
            //Now I need to compare the plots to see if the plot should expire and the difference in time.
           
     
  2. gigosaurus

    Supporter

    You will have to schedule a task to check if any plots have expired. Set the task to run when the server starts and then every hour or something.
    Something like this:
    Code (Text):

    Bukkit.getScheduler().runTaskTimer(plugin, new Runnable() {
        @Override
        public void run() {
            Date currentTime = new Date(System.currentTimeMillis());
            //loop through each plot's expire time
            ...
                if (currentTime.after(expiredTime)) {
                     //plot has expired
                }
            ...
       }
    }, 0, 60 * 60 * 20);
     
     
    #2 gigosaurus, Apr 30, 2015
    Last edited: Apr 30, 2015
  3. I am trying to figure out how to find the difference. Something like currentTime - expireTime. I know I can do
    Code (Text):
    if(date.after(date)) {
    //Stuff
    }
     
  4. gigosaurus

    Supporter

    If you read the code I provided, I showed you how you can do that. Check the if statement between the ...