Formatting time

Discussion in 'BungeeCord Plugin Development' started by JoeyPlayzTV, Jun 30, 2020 at 9:39 PM.

  1. Hey, i have a BannSystem which shows time like "1d 1h 1min 1s" but if i ban a player for only 1 day, it looks so
    "1d 0h 0min 0s". I would like it that only the time that is there is shown. Also only "1d"

    My idea was using a StringBuilder and only append if the x value is bigger than 0 but that is not so nice because if the day is 0 it would add a space in front. Has someone an idea to realize that?

    My idea

    Code (Java):
    StringBuilder builder = new StringBuilder();
           
            long time = 60000;
           
            Duration dur = Duration.ofMillis(time);
            long min = dur.toMinutes();
            dur = dur.minusMinutes(min);
            long sec = dur.getSeconds();
            dur = dur.minusSeconds(sec);
           
            if(min > 0) {
                builder.append(min + "m ");
            }
            if(sec > 0) {
                builder.append(sec + "s");
            }
            System.out.println(builder.toString());




    Thanks for ideas
     
    #1 JoeyPlayzTV, Jun 30, 2020 at 9:39 PM
    Last edited: Jun 30, 2020 at 10:03 PM
  2. How do you store the date? If you use System.currentTimrMillis() you could use the Java SimpleDateFormat.
     
  3. Yea it is System.currentTimeMilis()
     
  4. I mean not date i mean a duration like 1d 1h etc but replacing the units with "" if there are not filled

    A ban with a duration of 1 day and 1 hour must be so

    -> 1d 1h

    and a ban with a duration of only one day must be

    -> 1d and not 1d 0h
     
  5. Like so

    Code (Java):
    StringBuilder builder = new StringBuilder();
         
            long time = 60000;
         
            Duration dur = Duration.ofMillis(time);
            long min = dur.toMinutes();
            dur = dur.minusMinutes(min);
            long sec = dur.getSeconds();
            dur = dur.minusSeconds(sec);
         
            if(min > 0) {
                builder.append(min + "m ");
            }
            if(sec > 0) {
                builder.append(sec + "s");
            }
            System.out.println(builder.toString());
     
  6. I have solved this in this way. I will return a "pretty" duration not a date or a time

    This works. The trim() removes the spaces ad the begin and the end if there are spaces.

    Code (Java):
    StringBuilder builder = new StringBuilder();
           
            long time = 64000;
           
            Duration dur = Duration.ofMillis(time);
            long min = dur.toMinutes();
            dur = dur.minusMinutes(min);
            long sec = dur.getSeconds();
            dur = dur.minusSeconds(sec);
           
            if(min > 0) {
                builder.append(min + "m ");
            }
            if(sec > 0) {
                builder.append(sec + "s");
            }
           
            System.out.println(builder.toString().trim());
     
  7. Oh I'm sorry. My bad. I thought you wanted to display a date and not a duration. Sorry
     
    • Funny Funny x 1
  8. Yea but it is very difficult to explain :)