Solved Time whne player first joined

Discussion in 'Plugin Development' started by Pizza371, Aug 1, 2014.

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

    Pizza371

    So... I tried to calculate when a player first joined in days, hours, minutes and seconds.
    it went horribly wrong.

    this is my code:
    Code:
    long time = p.getFirstPlayed();
    long days = TimeUnit.MILLISECONDS.toDays(time), hour = TimeUnit.MILLISECONDS.toHours(time), minute = TimeUnit.MILLISECONDS.toMinutes(time), second = TimeUnit.MILLISECONDS.toSeconds(time);
     
    String timeFormatted = String.format(this.prefix + ChatColor.RED + p.getName() + ChatColor.RESET + "joined " + ChatColor.RED + "%d" + ChatColor.RESET + " days, " + ChatColor.RED + "%d" + ChatColor.RESET + " hours, " + ChatColor.RED + "%d" + ChatColor.RESET + " minutes, and " + ChatColor.RED + "%d" + ChatColor.RESET + " seconds ago.", days, hour - TimeUnit.DAYS.toDays(days), minute - TimeUnit.HOURS.toMinutes(hour), second - TimeUnit.MINUTES.toSeconds(minute));
    return timeFormatted;
    
    I can't see why it isn't working. Doesn't the method supposed to return the time he joined?
     
  2. Offline

    AoH_Ruthless

    Pizza371
    It works for me.

    What have you tried? Have you debugged? Any error log? You have to let us know more :)


    Note: Remember that Player#getFirstPlayed() returns 0 if the player is on the server for the first time.
     
  3. Offline

    Pizza371

    AoH_Ruthless
    Yeah, sorry - I was in a bit of a rush and I thought it would be a simple answer since It seemed to work when I didnt use the method.

    it displays as something like this: http://prntscr.com/48k5jr
    I'm really confused.

    No errors, I tried my code with different time and it seemed to work.

    Thanks!
    Pizza.
     
  4. Offline

    AoH_Ruthless

    Uh .. so is it solved?
     
  5. Offline

    Pizza371

    AoH_Ruthless
    Nono :p I realized it looks that way.

    If I use a time in milliseconds I set it seems to work, but OfflinePlayer#getFirstPlayed() doesn't
     
  6. Offline

    AoH_Ruthless

    Pizza371
    OfflinePlayer#getFirstPlayed() returns the time between player login first and January 1, 1970 in UTC at Midnight.

    So, to get that time, use the following:

    Code:java
    1. long first = Player#getFirstPlayed(); // returns time in ms from 1/1/1970 12:00 UTC until FIRST JOIN
    2. long current = System.currentTimeMillis() // returns time in ms from 1/1/1970 12:00 UTC until NOW
    3.  
    4. long diff = (current - first); // This returns the amount of milliseconds between now and player first join
    5.  
    6. // Your code to get time.
     
    leon3001 likes this.
  7. Offline

    Pizza371

Thread Status:
Not open for further replies.

Share This Page