Time format

Discussion in 'Spigot Plugin Development' started by Snowmite, Jun 15, 2016.

  1. So I am making a tempban plugin where it would print a nice format for the player that is banned when they try to join. When I ban them for 1 day, it prints "1 day(s) 24 hour(s)", how would I make it say "1 day(s) 0 hour(s)"?

    My Code: http://hastebin.com/ufawivarex.cpp
     
  2. with math and loops...
     
  3. So helpful.. Can you explain how?
     
    • Agree Agree x 1
  4. You cannot bump until its 24 ours since your last reply.
     
  5. Oh sorry, can someone just please answer?
     
  6. This is a class I use:

    Code (Text):
    package us.universalpvp.utils;

    import java.util.HashMap;
    import java.util.Map;
    import java.util.regex.Pattern;

    public enum TimeUnit {
        SEC("second", 1L, 's'), MIN("minute", 60L, 'm'), HOUR("hour", 3600L, 'h'), DAY("day", 86400L, 'd'), WEEK("week",
                604800L, 'w');

        private final String name;
        private final long ms;
        private final char unit;
        private static final Map<Character, Long> convertion;
        private static final TimeUnit[] order;
        private static final Pattern isLong;

        static {
            convertion = new HashMap();
            order = new TimeUnit[] { WEEK, DAY, HOUR, MIN, SEC };
            isLong = Pattern.compile("[0-9]+");
            TimeUnit[] arrayOfTimeUnit;
            int j = (arrayOfTimeUnit = values()).length;
            for (int i = 0; i < j; i++) {
                TimeUnit unit = arrayOfTimeUnit[i];
                convertion.put(Character.valueOf(unit.unit), Long.valueOf(unit.ms));
            }
        }

        private TimeUnit(String name, long ms, char unit) {
            this.name = name;
            this.ms = ms;
            this.unit = unit;
        }

        public String addUnit(int x) {
            String r = this.name;
            if (x > 1) {
                r = r + "s";
            }
            return x + " " + r;
        }

        private static long convert(char c) {
            if (convertion.containsKey(Character.valueOf(c))) {
                return ((Long) convertion.get(Character.valueOf(c))).longValue();
            }
            return 0L;
        }

        public static String toString(long time) {
            StringBuilder sb = new StringBuilder();
            TimeUnit[] arrayOfTimeUnit;
            int j = (arrayOfTimeUnit = order).length;
            for (int i = 0; i < j; i++) {
                TimeUnit unit = arrayOfTimeUnit[i];
                if (time >= unit.ms) {
                    int t = (int) Math.floor(time / unit.ms);
                    sb.append(unit.addUnit(t)).append(" ");
                    time -= t * unit.ms;
                }
            }
            return sb.toString().trim();
        }

        public static long toLong(String s) {
            long fromString = 0L;
            String[] arrayOfString;
            int j = (arrayOfString = s.split(" ")).length;
            for (int i = 0; i < j; i++) {
                String t = arrayOfString[i];
                char c = t.charAt(t.length() - 1);
                t = t.substring(0, t.length() - 1);
                if (isLong.matcher(t).matches()) {
                    fromString += Long.parseLong(t) * convert(c);
                }
            }
            return fromString;
        }
    }
     
  7. The problem you have is here:
    Code (Text):

    int days = seconds/86400;
    int hours = seconds/3600;
     
    If you look at your code, you are taking the original time to get hours, but you aren't then subtracting from it.

    Edit:
    Solution:
    Code (Text):

    int remainder = 0;
    int days = seconds/86400;
    remainder = seconds%86400;
    int hours = remainder/3600;
    remainder = remainder%3600;
    int mins = remainder/60;
    int secs = remainder%60;
     
    (Didn't test, maybe another error)
     
  8. TO use it do:

    Code (Text):
    TimeUnit.toString(10);
    and that would show as 10 secnds.
     
  9. Do you know how to use the % or remainder operator? http://www.cafeaulait.org/course/week2/15.html

    Let's say the number of seconds are 131. If we divide that by 60 and round down, we get 2. Then we use the remainder operator and do 131 % 60. We should get 11. There you have it, 2 minutes and 11 seconds. I'm sure you can figure out how to do hours. (Hint: use 60 * 60 instead of 60)
     
  10. If you took a look at his code (pastebin link), he did use the operator with right understanding of what it does.