[Tutorial]Better Time Formatting

Discussion in 'Resources' started by DevRosemberg, Sep 7, 2013.

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

    DevRosemberg

    Hi Everyone, i am DevRosemberg and i bring this guide to you to show you how to turn this horrible 120 seconds Into this:
    2:00


    Code by macguy8
    Guide by DevRosemberg

    Using a really simple Util, this is the code:
    Code:java
    1. public static String formatIntoHHMMSS(int secs)
    2. {
    3. int remainder = secs % 3600;
    4. int minutes = remainder / 60;
    5. int seconds = remainder % 60;
    6.  
    7. return new StringBuilder().append(minutes).append(":").append(seconds < 10 ? "0" : "").append(seconds).toString();
    8. }



    Now i am going to create a simple Util named TimeUtil:

    Code:java
    1. public class TimeUtil {
    2.  
    3. public static String formatIntoHHMMSS(int secs)
    4. {
    5. int remainder = secs % 3600;
    6. int minutes = remainder / 60;
    7. int seconds = remainder % 60;
    8.  
    9. return new StringBuilder().append(minutes).append(":").append(seconds < 10 ? "0" : "").append(seconds).toString();
    10. }
    11.  
    12. }


    When you want to turn your time into a Human readable format using this code simply do:

    Code:java
    1. Int time = 60;
    2. String timeString = TimeUtil.formatIntoHHMMSS(GameUtil.this.time);
    3. //And when you check what time is and broadcast a message do:
    4. If (GameUtil.this.time == 60) {
    5. Bukkit.broadcastMessage(timeString + “left!);
    6. }


    This code will help you turn that horrible 120 into 2:00
    I hope his helped you and See you in another Guide!

    Regards, Dev
     
    jacklin213 likes this.
  2. Offline

    jacklin213

    Great idea
     
  3. Offline

    DevRosemberg

  4. Offline

    Rocketboy901

  5. Offline

    DevRosemberg

  6. Offline

    macguy8

    My code :c
     
  7. Offline

    Rocketboy901

    o geez, le sexy wild catfight now! (I wish I even knew what this does)
     
  8. Offline

    KollegahDerBoss

    new java.text.SimpleDateFormat("hh:mm:ss")).format(new java.util.Date(seconds * 1000L));
    done...
     
  9. Offline

    DevRosemberg

  10. Offline

    macguy8

    Actually, yes, the 2 methods do almost the exact same thing, but a few changes would need to be made to Aangiix3 's method for it to work.
     
  11. Offline

    DevRosemberg

  12. Offline

    macguy8

    How wouldn't said code work...?
     
  13. Offline

    DevRosemberg

Thread Status:
Not open for further replies.

Share This Page