Time & Math [HH:MM:SS]

Discussion in 'Plugin Development' started by BungeeTheCookie, Jul 5, 2014.

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

    BungeeTheCookie

    Hey Bukkiteers! I am trying to parse a long into a time string. For example, if the long is 7200, I am trying to make it return 2:00:00. Or if I have 6001, I want it to return 1:40:01. Since the long is in seconds, I am making the hour 3600 [how many seconds that are in an hour] to divide the time long.

    PHP:
    private String parseTime(long time){
            
    Integer hour = (int)Math.floor(time 3600);
            
    Integer minute = ((int)Math.floor(time 60)) - (hour 60);
    How do I find the seconds, and is my time code accurate?

    Nevermind, I found a class that did that for me.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  2. Code:java
    1. Date date = new Date();
    2.  
    3. int hours = date.HOUR;
    4. int minutes = date.MINUTES;
    5. int seconds = date.SECONDS;
     
  3. Offline

    BungeeTheCookie

    FisheyLP
    Alright, I will take a peek at yours, I ddin't solve mine :p

    Nevermind, I unsolved it..
    FisheyLP
    How does it tell to exclude the years and months and days?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  4. write date. and then press Ctrl + Spacebar to see all the things you can do with it
     
  5. Offline

    AoH_Ruthless

    BungeeTheCookie
    https://gist.github.com/AoHRuthless/d727bc6cf1c42a56107c

    That takes seconds. So, if you are working with something like ticks, just pass through your long * 20. If you are working with system time in milliseconds, obviously do long * 1000. Those are the two possibilities I thought of, but it could be anything in your case.

    Edit: I don't know if that's what you were looking for.
     
  6. Offline

    Traks

    - Divide time by 3600 to get the hours (as int!)
    - Substract the hours * 3600 from time
    - Divide the result of that by 60 to get the number of minutes (as int!)
    - To get the number of seconds, substract the minutes * 60 from the result of the second point

    As a side note, converting to int gets rid of decimals. You could also use Math#floor(double), although this will have different behaviour for negative numbers
     
  7. Offline

    BungeeTheCookie

    Traks AoH_Ruthless FisheyLP
    I just did this, but will it work?

    Integer second = (int)(Math.IEEEremainder((time - ((hour * 3600) + (minute * 60))) , 60));
     
  8. Offline

    Garris0n

  9. Offline

    BungeeTheCookie

  10. Offline

    PogoStick29

    Don't use Date, use Calendar.

    Calendar cal = Calendar.getInstance();
    cal.set(time); // time is your long. I think that's the method.
     
    Garris0n likes this.
  11. Offline

    BungeeTheCookie

    PogoStick29
    So I just use Calendar.getInstance().setTimeInMillis(7200 * 1000);
    and I can use Calendar.SECOND or Calendar.MINUTe and it will work?
     
  12. Offline

    PogoStick29

    For the second part, you'd do

    int (or whatever) seconds = cal.get(Calendar.SECOND);

    Because all of those constants represent keys that when passed to the get() method return the correct values. I know, it's a weird way of doing it.
     
  13. Offline

    Garris0n

    I should've probably linked to this, although you could've just googled "how to use dateformat java".
     
  14. Offline

    BungeeTheCookie

    But the first part is correct? And this wouldn't throw off any other things associating with System.nanoTime() or other areas where Calendar.getinstance() is involved [I know Calendar cannot be instantiated so we can't use new Calendar()) so I am wondering if this will affect all accessed Calendar classes... PogoStick29

    Ok I am looking at it now.

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

    PogoStick29

    It will not. Calendar is strange, but getInstance() returns a new Calendar.

    http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html

    Like other locale-sensitive classes, Calendar provides a class method, getInstance, for getting a generally useful object of this type. Calendar's getInstance method returns a Calendar object whose calendar fields have been initialized with the current date and time.
     
  16. Offline

    BungeeTheCookie

    Thanks.
     
  17. Offline

    Garris0n

    If you wanted to instantiate it, you would use a GregorianCalendar. Calendar#getInstance() just picks the right type for you.
     
    PogoStick29 likes this.
Thread Status:
Not open for further replies.

Share This Page