Solved Check the date every 24 hours

Discussion in 'Plugin Development' started by Luke_Lax, Jun 30, 2013.

Thread Status:
Not open for further replies.
  1. Offline


    Hey everyone, I've started a new project and I've come across an issue which I can't find anything on, except for one post (here) which confused me because the OP wanted the same thing but for his event to happen at 7pm everyday, I want the same thing but every 24 hours.

    In the post that I found they were using the Calendar which is documented on the JavaDocs (here) mixed with "Bukkit.scheduleSyncRepeatingTask(plugin, new Runnable()" which I know nothing of.

    Please help, I'd appreciate it so much!

    Please don't be nasty to me, like someone was on my last post which was my first. I'm a nice person who's just after some help.
  2. Offline


    Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new BukkitRunnable() {
    public void run() {
            //calendar stuff
    }, 100, 86400*20);
  3. Offline


    Compressions I assume "100, 86400*20);" is to check it every 24 hours but what would go inside "//calendar stuff"?

    This has confused me, sorry I really hate to be dependant but I'm clueless as to how to implement this into my plugin and to what "//calendar stuff" means :|

    I was attempting to get and check the date like this:
    1. Date now = new Date();
    2. SimpleDateFormat format = new SimpleDateFormat("dd");
    3. if (format(now) == 30) {

    But now I'm confused as to where even something like that would go, like you have "public boolean onCommand" what would mine be to simply check the date? :/ Pleas help

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Jun 2, 2016
  4. Offline


    Do you really want to check the date? Or do you just want to run something every 24 hours?

    If you want the first, you can get the current date and time whenever you need it like this:
    (this is how i get time and date for making log entries in one of my plugins)
    long currTime = ((Calendar)Calendar.getInstance()).getTimeInMillis();
    final SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
    String logTime = sdf.format(new Date(currTime));
    You may want to set the date format to your countries usual format.
    From there, you can parse the string and check the date.
    I don't know if there is a simpler way.

    If you only need to run some method every 24 hours, there is no need to check the date.
    Just do what you want to do in the timers run() method.

    Of course 24 hours is a long time. So if your server crashes or gets stopped/restarted, the time will start over from 0.
    A way to prevent this, and the way I would prefer here, is this:
    Get the currentTimeInMillies() + 24 hours (in ms) and save that timestamp to a yml file.
    This will be your scheduled time for the next day to execute your code.
    Then check the current time whenever you want to (with a timer that runs every minute or so, or use an event)
    and look if the current time is > your scheduled time.
    If so, execute your code and re-schedule your timer to run the next day.
    So you do not have to fiddle around with dates, time formats, time zones, and stuff.
  5. Offline


    CubieX you clearly know what you're talking about hehe, I DO in fact need to check the date: what I want for example is:

    24 hours is up: Is the date the 30th?
    If so do { }
    else start the 24 hours again (wait until tomorrow to check)

    So on and so forth.

    I say a 24 hour timer that runs all the time because I can't think of any other way to get the plugin to check the date without someone having to type a command.

    Do you think you could piece together my puzzle, I have 3 bits of information given to me of which I have no idea as to what to do with them, I get it will look something like this (which I must stress, this does NOT work):
    1. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new BukkitRunnable() {
    2. public void run() {
    3. Date now = new Date();
    4. if (format.format(now) == 30) {
    5. //do the action which must occur when the date is one of the dates specified in the config (not included yet)
    6. }
    7. }
    8. }, 100, 86400*20);
    9. }

    Please would someone help, I'd appreciate it SO much!
  6. Offline


    I would just suggest starting a task in your plugin's onEnable() with a delay equivalent to the seconds from the current time until 7 P.M. times 20 ticks. That way, it will be reload- and restart-proof, and will only run once per day. Granted, you still need to find the Unix time for the next 7 P.M. (if that makes any sense), but you won't need to do any comparisons.
  7. Offline


    Hmm interesting idea, could I have an example?

    Bump :Z Please help! If someone, anyone ;-; could show me how to simply Check the date every 24 hours and if it equals a certain date //do something

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Jun 2, 2016
  8. Offline


    Answer is here on the first post (Stackoverflow)
    <Edit by Moderator: Redacted bit url>
    So save the old date in a config and make a thread run once then on a 30 minute delay (Or whatever you want schedule it as). Check if they are equal then do whatever.
    Last edited by a moderator: Feb 16, 2017
    Eats_Rainbows likes this.
  9. Offline


    MCForger but how does that check the date? I don't have an issue with finding the date. I could place the event in the onEnable() but then I'd need to restart my server everyday or I could add a command which would start the check but again this would require someone to manually type a command to check defeating the point of automated. I understand that you're all trying to help me and I appreciate that a lot! but giving me "let me google that for you" links to things I already know aren't useful, I'm nooby with all of this, it's new plugin territory for me and it seems everyone can link random bits of information but not piece together a working example :(
  10. Offline


    Alright did not see you where new (Should have read more myself), I apologize for that link. So on first run check the config for a date that you saved there to string and if there is not one there then getInstance() of the current calendar and save it there. Then make a runnable thats runs once and follows up every 30 minutes doing a check for the same date.
    Getting the date:
    1. DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    2. //get current date time with Date()
    3. Date date = new Date();
    4. System.out.println(dateFormat.format(date));
    6. //get current date time with Calendar()
    7. Calendar cal = Calendar.getInstance();
    8. System.out.println(dateFormat.format(cal.getTime()));

    Comparing dates:
    1. if(todayDate.after(historyDate))
    2. {
    3. // In between
    4. }
  11. Offline


    MCForger This may be a big ask, and if it is please just say as I understand you're trying to help :) but do you think you could show me an example that will check if it's, just for example sakes, the 30th of any given month and if it is to print the line "It's the 30th." ? A fully pieced example will be a lifesaver for me :)

    Oh that example wasn't there when I wrote this^ :p So will this constantly check if it is one of the dates?

    Ok this is what I've got so far and I'm being flooded with errors :|
    Show Spoiler

    Which, as my lucks shows, does not work at all - I'm so confused, I think I need to ask for no more guesswork I can't figure this out :(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Jun 2, 2016
  12. Offline


    Combining the other peoples posts here, this should be what you need(Its very hard to understand what you are trying to do though)

    As you already stated, you can get the dates no problem so I did not include that part in the code

    Put this code in your onEnable:
    2. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new BukkitRunnable() {
    3. public void run() {
    4. //Any code here will run every 24 hours(Starting from when the plugin is loaded)
    5. if(todayDate.after(historyDate))
    6. {
    7. // In between
    8. }
    9. }
    10. }, 100, 86400*20);
    Luke_Lax likes this.
  13. Offline


    bittiez Thank you so much! I just needed someone to help me out by piercing the information together, I really appreciate everyones help!
  14. Offline


    I suggest learning some java.
    Eats_Rainbows likes this.
  15. Offline


    Post a tutorial for what he needs, or actually help him through his problems. Posting for the person to "learn java" doesn't do anyone any good.
  16. Offline


Thread Status:
Not open for further replies.

Share This Page