How to format a String arg time to milliseconds?

Discussion in 'Plugin Development' started by Colby l, Feb 11, 2014.

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

    Colby l

    I was wondering how I can convert an arg of '1h2m3s' to milliseconds (or nanoseconds, whichever is less server stressful).

    If a player uses '/flyban 1h' or '/flyban 1d2h3m12s' I need the loop/code to extract the numbers, and differentiate between the times. Then it returns the milliseconds it equals.

    Java 8 has a super easy way to do this, but I don't think most servers run Java 8 yet.

    Thanks

    I've updated the title to more appropriately address the issue I have.

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

    viper_monster

  3. Offline

    Maurdekye

    Colby l You don't need Java 8, you can use regular expressions.
    You can use this code to grab the days from the argument;
    Code:java
    1. int days = 0;
    2. Pattern getDays = Pattern.compile("(\\n*)d");
    3. Matcher dayMatch = getDays.matcher(args[0]);
    4. if (dayMatch.find())
    5. days = Integer.parseInt(dayMatch.getGroup(1));

    Simply repeat for each of hours, minutes and seconds.
     
Thread Status:
Not open for further replies.

Share This Page