Solved Convert ticks to minutes and seconds.

Discussion in 'Plugin Development' started by TheEnderCrafter9, Sep 6, 2016.

Thread Status:
Not open for further replies.
  1. Hi, what would be the best way to convert ticks to minutes and seconds?

    Every second is 20 game ticks.

    I'm thinking of dividing the amount of ticks by 20, then converting it into minutes with the seconds being the modulo, however, I would like to hear from the community if there are better ways.
     
  2. Offline

    DoggyCode™

    Well, it's not hard. In java, you can use maths. So

    * = times

    Code:
    int minute = 20*60;
    int hour = minute*60
    int day = hour*24
    int week = day*7
    int month = week*4
    int year = month*12
    
    day = 20*60*24
    If that is what you are looking for.

    EDIT:
    Or do you mean for example directly convert 100 ticks to seconds and minutes for example? Well.

    For seconds, you must divide the ticks by 20. For example:
    Code:
    100 / 20 = 5 seconds
    so 100 ticks is 5 seconds.

    For minutes, you must divide the ticks by 1200 (20*60), in other words, one minute in ticks. For example:
    Code:
    5000 / 1200 = 4.16 minutes
    so 5000 ticks is 4.16 minutes.
     
    Last edited: Sep 6, 2016
  3. Offline

    FabeGabeMC

    A second in ticks = 20 ticks,
    so:
    1 minute = 20 * 60,
    an hour = 20 * 60 * 60,
    a day = 20 * 60 * 60 * 24
    a year = 20 * 60 * 60 * 24 * 365 (or 366 for leap years)
     
  4. @FabeGabeMC @DoggyCode™ I know that you can do math in java, I'm looking for a way not to get decimal points but minutes and seconds. Like 4:30, not 4.5 .
     
  5. Offline

    DoggyCode™

    60 = 3 seconds
    Well, that's not hard at all :)

    I came up with this and it works:
    Code:java
    1. int ticks = 4500; //total ticks
    2.  
    3. long minute = ticks / 1200;
    4. long second = ticks / 20 - minute*60;
    5.  
    6. String s = "" + Math.round(minute) + ":" + Math.round(second);
    7.  
    8. System.out.println("" + (ticks) + " tick(s) = " + s + " second(s).");


    I did this and it works perfectly.

    Output:
    Code:
    4500 tick(s) = 3:45 second(s).
     
    TheEnderCrafter9 likes this.
  6. @TheEnderCrafter9

    Ticks to minutes/hours
    Code:java
    1. TimeUnit.MINUTES.toSeconds(1) * 20


    Minutes/hours to ticks
    Code:java
    1. TimeUnit.SECONDS.toMinutes(ticks * 20)
     
    DoggyCode™ likes this.
  7. Offline

    DoggyCode™

    Code:java
    1.  
    2. I think he wanted it like I did.
     
  8. @DoggyCode™ I didn't read the replies I only read the title :p
     
  9. Offline

    DoggyCode™

    Same, at the beginning :)

    @TheEnderCrafter9

    You can do a simple if/else statement if you don't want to include the ":" when minutes is 0:
    Code:java
    1. int minute = Math.round(ticks / 1200);
    2. int second = Math.round(ticks / 20 - minute*60);
    3.  
    4. String s = null;
    5.  
    6. if(minute > 0) {
    7. s = "" + (minute) + ":" + (second);
    8. } else {
    9. s = "" + (second);
    10. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Sep 6, 2016
Thread Status:
Not open for further replies.

Share This Page