How can I convert minecraft long time to real hours and minutes?

Discussion in 'Plugin Development' started by EvilPeanut, Jan 14, 2013.

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

    EvilPeanut

    Hello,

    I need to get the hours and minutes from the minecraft long time (loc.getWorld().getTime()).
    How can this be done?

    Thanks,
    Reece
     
  2. Offline

    !Phoenix!

    From Bukkit Javadoc

    hours = Math.floor(time/1000)
    minutes = (time % 1000) / 1000 * 60

    ... I would say
     
  3. Offline

    EvilPeanut

    Your method for returning the hours is working, thanks. However, your method for returning the minutes doesn't work.
     
  4. Offline

    !Phoenix!

    Code:java
    1. long time = 13250L;
    2. int hours = (int) Math.floor(time / 1000);
    3. int minutes = (int) ((time % 1000) / 1000.0 * 60);
    4.  
    5. System.out.println(time + " as rl-time is " + hours + ":" + minutes);

    Prints: "13250 as rl-time is 13:15"

    I made a floating point value out of one of the 1000's (-> 1000.0) so that java is working with floats here and not int/long.
    See: Java: Dividing 2 int's makes an int?
     
  5. Offline

    raGan.

    His method is mathematically correct. You must be doing something wrong.
     
  6. Offline

    !Phoenix!

    EvilPeanut likes this.
  7. Offline

    EvilPeanut

    Thanks, got it working!
     
  8. Offline

    lDucks

    You can use a Calendar to do it, this is the method I always use:

    Code:
    static Calendar cal = new GregorianCalendar();
     
    public static String convertLongToDate(long time){
    cal.setTimeInMillis(time);
    return cal.getTime().toString();
    }
    
    You can also get the hours, minutes, etc. from this...

    You do:

    Code:
    cal.get(Calendar.HOUR_OF_DAY);
    EDIT:

    Just realized you want MineCraft time, not real world time. But this calendar thing is pretty cool too if you ever need it ;P
     
  9. Offline

    fromgate

    Here is my procedure :)
    Code:java
    1. public String timeToString(long time) {
    2. int hours = (int) ((time / 1000 + 8) % 24);
    3. int minutes = (int) (60 * (time % 1000) / 1000);
    4. return String.format("%02d:%02d", hours, minutes);
    5. }
     
  10. Offline

    !Phoenix!

    To explain what fromgate does here: He shifts the time by eight hours forward, because, as you may already noticed, the world-time zero (0) is somewhere around sunrise and not exactly at midnight. I don't know where he got that number (8h) from, but it could fit pretty well (Thumbs up). Notice that not using floor() on this shifts the time a little bit further.
    Additionally he arranged the minutes-calculation in a way that the usage of floating point values is superfluous.

    Where do you got the interval from, fromgate?
    • Looking at your code it seems like the clock will jump from 0:29 to 1:30, could that be?
    • Not using floor() on the calculation of minutes allows the value '60' to appear, which would be the next hour. But I have the same problem with my code as I already hinted at.
    EvilPeanut, this could help you, too: Minecraft Wiki - Day-night cycle

    Plus let me implement the issues mentioned, I'll stick to the style of fromgate's function:
    Code:java
    1. public String timeToString(long time) {
    2. int hours = (int) ((Math.floor(time / 1000.0) + 8) % 24); // '8' is the offset
    3. int minutes = (int) Math.floor((time % 1000) / 1000.0 * 60);
    4. return String.format("%02d:%02d", hours, minutes);
    5. }
     
    fromgate and EvilPeanut like this.
Thread Status:
Not open for further replies.

Share This Page