Util [Resource] Formatting Time!

Discussion in 'Resources' started by AoH_Ruthless, May 11, 2014.

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

    AoH_Ruthless

    Hate seeing some long meaningless integer like "4849 seconds" in your plugin? Well, this resource allows you to format time with ease: The method auto-magically formats your time into days, hours, minutes, and seconds.

    For example:

    90289 seconds will be input and printed out as 1 day 1 hour 4 minutes 49 seconds

    To call the method in another class, just do TimeUtil.format(integer as seconds):

    Code:java
    1. public void timeSurvived(Player p) {
    2. int time = 48937;
    3.  
    4. p.sendMessage("You have survived for" + TimeUtil.format(time) + "!");
    5. }


    Download the class here.

    I hope you use it and like it!
     
    Gater12 likes this.
  2. Offline

    Onlineids

    Nice, this will come in handy formatting tempban and tempmute ;)
     
    AoH_Ruthless likes this.
  3. Offline

    MiniDigger

    Well, its seems like there is something wrong:
    Code:java
    1. final Date d1 = new Date();
    2. Bukkit.getScheduler().runTaskLater(Core.getInstance(), new Runnable() {
    3.  
    4. @Override
    5. public void run() {
    6. Date d2 = new Date();
    7. long time = d2.getTime()-d1.getTime();
    8. System.out.println("TIME PASSED: " + TimeUtil.formatTime((int) time));
    9. }
    10. }, Integer.parseInt(args.getArgs()[0]));

    gives me : TIME PASSED: 2 Stunden 46 Minuten 40 Sekunden (translated that into german, it is hours,minutes and seconds in english)
     
  4. Offline

    mazentheamazin

    AoH_Ruthless
    There's another way to do it, I deem that it looks better as for what it returns, and is a bit shorter, in terms of amount of lines.

    Code:java
    1. public String ticksToTime(long ticks) {
    2. long fullTime = timeTics / 20;
    3. long mins = (int) TimeUnit.SECONDS.toMinutes(fullTime) % 60;
    4. long secs = (int) TimeUnit.SECONDS.toSeconds(fullTime) % 60;
    5.  
    6. return String.format("%02d:%02d", mins, secs);
    7. }
    8.  
    9. public String secondsToTime(int seconds) {
    10. long mins = (int) TimeUnit.SECONDS.toMinutes(seconds) % 60;
    11. long secs = (int) TimeUnit.SECONDS.toSeconds(seconds) % 60;
    12.  
    13. return String.format("%02d:%02d", mins, secs);
    14. }
     
  5. Offline

    raGan.

    You know times are hard when you need to provide people with this kind of things.
     
  6. Offline

    AoH_Ruthless

    mazentheamazin
    Yours is shorter because you do not incorporate days or hours. Longs are better if you want obscenely large times, otherwise your parameter could just be an integer * 20

    MiniDigger
    The way it is parsed it will not tell you days if the amount of days is 0

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

    MiniDigger

    AoH_Ruthless I got my error. I passed miliseconds and not seconds which caused the wrong values.
     
  8. Offline

    BungeeTheCookie

    Not to burst your time bubble, but Dragonphase made something almost exactly like this.
     
  9. Offline

    AoH_Ruthless

    BungeeTheCookie
    It doesn't burst my bubble, the more resources the merrier :p
     
  10. Offline

    Regablith

    Amazing job!
     
  11. Offline

    BungeeTheCookie

    Time to get out the pin.
     
    AoH_Ruthless likes this.
  12. Offline

    Bloxcraft


    Date.getTime() doesn't return seconds IIRC
     
  13. Offline

    MiniDigger

    well, I allready got my stupid error :D
    But thanks anyway
     
Thread Status:
Not open for further replies.

Share This Page