[Tutorial] Comparing Two Points In Time (dates)(delays)

Discussion in 'Resources' started by MrAwellstein, Apr 30, 2014.

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


    Alright, I've seen enough people ask this so I'm going to be writing my way of telling you how much time has passed. I will not be writing a perfect example for you, and you have to figure out where to place your dates (I forgot to mention, java.util.Date), how you store them, and how you access them. This example only shows how to compare time. Please don't complain that you don't know how to do something, or how this helps or whatever, because this is how delays are created. I will be giving you a gift of my own method for comparing dates, to make it easier on you.

    This here is the DateChecker class that I created. Ignore the sloppy coding, and just pay attention to how you can use it. It's quite simple.
    2. public final static class DateChecker{
    3. public final static boolean IsTime(Date d1, Date d2,double time, DateType t){
    4. return (getDifference(d1,d2,t)>=time);
    5. }
    6. public final boolean isItTime(Date d, double time, DateType t){
    7. return (getDifference(d,new Date(),t)>= time);
    8. }
    9. public final static double timeRemaining(Date d, double time, DateType t){return timeRemaining(d,new Date(),time,t);}
    10. public final static double timeRemaining(Date d, Date d2, double time, DateType t){
    11. double remander = (time - getDifference(d,d2,t));
    12. if(remander < 0){return 0;}
    13. return remander;
    14. }
    15. public final static double getDifference(Date d1, Date d2, DateType t){
    16. switch(t){
    17. case milisecond: return ((d2.getTime()-d1.getTime()));
    18. case second:return ((d2.getTime()-d1.getTime())/1000.);
    19. case minute: return ((d2.getTime()-d1.getTime())/1000./60.);
    20. case hour: return ((d2.getTime()-d1.getTime())/1000./60./60.);
    21. case day: return ((d2.getTime()-d1.getTime())/1000./60./60./24.);
    22. case week: return ((d2.getTime()-d1.getTime())/1000./60./60./24./7.);
    23. }
    24. return 0;
    25. }
    26. public static enum DateType{milisecond,second,minute,hour,day,week}
    27. }

    Now the first one that I will be showing you, will be getDifference(Date d1, Date d2, DateType t); which shows the difference between two dates. In this example we shall be seeing how long it takes from onLoad() of our plugin to get to onEnable()
    1. private Date date;
    2. @Override public void onEnable(){
    3. System.out.println("It Took "+DateChecker.getDifference(date, new Date(), DateChecker.DateType.second)+" seconds to get to onEnable()");
    4. }
    5. @Override public void onLoad(){
    6. date = new Date();
    7. }

    Note, the reason why we did new Date() for d2 is because new Date() creates a new Date object with the current time down to milliseconds.

    This here is an example (my example) of how to have it linked with events. This here will make it so that players have to wait 20 seconds before being allowed to use a command to teleport up 20 blocks. Note that most of the methods, such as getDate() or getPlayer() or setDate() you will have to create yourself, as this was part of a different class that I created that is here in the resources, used to create commands that didn't needed to be referenced in the plugin.yml (yes I am self advertising).
    2. if(DateChecker.isItTime(getDate(), .2, DateChecker.DateType.minute)){
    3. getPlayer().teleport(getPlayer().getLocation().add(0, 20, 0));
    4. setDate(new Date());
    5. }else{
    6. getPlayer().sendMessage("You Can't Use This Command Yet");
    7. }

    Note: I'm coming back later to write more examples that can be usable. If you have any issues please post here.

    If anyone wants to create a master date record keeper class with me, I'd be up to it.
    DoctorDark, Synapz and ArthurMaker like this.
  2. Offline


    Please take some time to format the code properly (note that when you make edits, you lose indentation, so keep a backup of your post in a textfile somewhere). Sloppy coding has absolutely no place in a tutorial. It is so much easier to read properly formatted code that signals intent. You're trying to help people - don't make it hard for them to read your helpful tip.

    Perhaps you should argue why java.util.Date is preferable to the much simpler System.currentTimeMillis() (if at all). Why should I use one of the most deprecation-ridden classes in the Java standard library when I can just get the current timestamp through a single method?
    1. private long start;
    3. @Override
    4. public void onEnable() {
    5. long end = System.currentTimeMillis();
    6. System.out.println("It took " + (end - start) / 1000 + " seconds to get to onEnable()");
    7. }
    9. @Override
    10. public void onLoad() {
    11. start = System.currentTimeMillis();
    12. }

    java.util.Date definitely has its uses, but I'm calling Maslow's hammer on this one.

    Finally, I'm curious as to why you choose to mark your static methods final?
    MrAwellstein likes this.
  3. Offline


    I actually completely forgot about System.currentTimeMillis(); but yeah that's acceptable as well. I'm probably going to create some resource here soon, and use that instead of dates.
    Finally I left my code sloppy for right now because I had to go and now I am on my phone. I plan on making it nicer when I come back.

    Why do you mark your public methods public?

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


    Argumentum ad hominem. All you had to do was answer the question, but I'll do it for you then.

    A non-static method marked final cannot be overridden. This can be useful in frameworks where a method of a base class is necessary for the framework to function, and still has to be part of the public interface of the class hierarchy. It is still possible for subclasses to call the method implicitly, but they cannot override it, making the method immune to specialization.

    So what's the deal with static methods and the final modifier? Static methods cannot be overridden, but they can be hidden. Marking a static method as final means it cannot be hidden (just like a non-static final method cannot be overridden), i.e. making a method with the same signature will be a compiler error. The problem with method hiding is that it is not subject to polymorphism. Let's assume I created a subclass of DateChecker and made static methods with the same signatures as the ones existing in DateChecker. If I was to now do this:
    1. DateChecker dc = new DateCheckerSubclass();
    2. dc.getDifference(...);

    The getDifference of DateChecker would be called instead of the method of the subclass! If the methods were non-static, it would be the method of the subclass, because the dynamic type of the object is the subclass. By marking static methods final, this confusing scenario can be avoided.

    How often does this scenario actually crop up? Almost never. Therefore, the final keyword can be safely omitted for static methods - especially utility classes, which are almost never subclassed anyway. Besides, if subclassing should be avoided, we would normally mark the class as final instead of its methods (like the org.bukkit.Bukkit class). The crux of the matter (and the reason I asked) is that it is completely unnecessary, and the purpose of a tutorial is to make things clearer, not complicate them. Leave out all the bloat and cut to the chase instead :)
    MrAwellstein and Wizehh like this.
  5. Offline


    Great explaination. Anyway I'll update this tutorial later today using longs rather than just using dates.
Thread Status:
Not open for further replies.

Share This Page