In need of a time util

Discussion in 'Plugin Development' started by DoggyCode™, Jan 20, 2017.

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

    DoggyCode™

    I need a util which returns a readable string for milliseconds coverted to years months weeks days hours minutes and seconds.

    But let's say if it was 42 hours, it wouldn't say: 0 years, 0 months, 0 weeks, 2 days, 0 hours, 0 minutes, 0 seconds.

    It would say: 2 days.

    Or if it was 2 months, 2 weeks, 3 days, 5 hours, and 20 seconds, it would say so. Basically, remove any value which has 0.
     
  2. Offline

    timtower Administrator Administrator Moderator

    @DoggyCode™ Math basically.
    And just checking which thing has 0.
    Or am I wrong?
     
  3. Offline

    DoggyCode™

    I have something like this, and yeh, it is basically just math.

    PHP:
    public static String formatMillis(long millis) {
            return 
    String.format("%d day(s), %d hour(s), %d minutes, and %d second(s)",
                    
    TimeUnit.MILLISECONDS.toDays(millis),

                    
    TimeUnit.MILLISECONDS.toHours(millis) -
                            
    TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(millis)),

                    
    TimeUnit.MILLISECONDS.toMinutes(millis) -
                            
    TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),

                    
    TimeUnit.MILLISECONDS.toSeconds(millis) -
                            
    TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))
            );
    }
    But it is not printing out what i said, as it would basically include a 0
     
  4. Offline

    timtower Administrator Administrator Moderator

    @DoggyCode™ Then calculate every value on its own.
    Then add them after each other.
     
  5. Offline

    DoggyCode™

    Yeh, maths is not really my strongest. What do you mean by calculating every value on its own? Like?:

    long years = TimeUnit.MILLISECONDS.toYears(millis),
    long months= TimeUnit.MILLISECONDS.toMonths(millis),
    long weeks = TimeUnit.MILLISECONDS.toWeeks(millis),

    ...

    ?
     
  6. Offline

    timtower Administrator Administrator Moderator

    @DoggyCode™ Like that yes.
    Then if they are not 0: add them to the value.
     
  7. Offline

    DoggyCode™

    Would I still use String.format?
     
  8. Offline

    timtower Administrator Administrator Moderator

  9. Offline

    JanTuck

    ">
    You can yes.

    Sendt fra min ALE-L21 med Tapatalk
     
  10. Offline

    DoggyCode™

    PHP:
    long second = (millis 1000) % 60;
            
    long minute = (millis / (1000 60)) % 60;
            
    long hour = (millis / (1000 60 60)) % 24;
            
    long day = (millis / (1000 60 60 60)) % 7;
            
    long week = (millis / (1000 60 60 60 60)) % 4;
            
    long month = (millis 1000 60 60 60 60 60) % 12;
            
    long year 0L;

            
    String time String.format("%02d : %02d : %02d : %02d : %02d : %02d : %02d"yearmonthweekdayhourminutesecond);
    I got this far, but my head is exploding rn from maths (I know :(, i might have some difficulities). The seconds, minutes, and hours works (because i didn't calculate it), but the rest is wrong, and im getting a overcalculation warning or something.
     
  11. Offline

    JanTuck

  12. Offline

    DoggyCode™

    Code:
            long x = millis / 1000;
            long seconds = x % 60;
    
            x /= 60;
            long minutes = x % 60;
    
            x /= 60;
            long hours = x % 24;
    
            x /= 24;
            long days = x % 7;
    
            x /= 7; // 7 days in one week
            long weeks = x % 4;
    
            x /= 4;
            long months = x % 12;
    
            x /= 12;
            long years = x;
    
            return String.format("%02d years : %02d months : %02d weeks : %02d days : %02d hours : %02d min : %02d sec", years, months, weeks, days, hours, minutes, seconds);
    The string returns negative values for an input of 1000*100952121 (3 years 2 months 1 week 4 days 19 hours 50 minutes and 55 seconds in millis)

    EDIT: it returns a positive now, but the issue is, i must have miscalculated something, as the output is wrong.

    EDIT2: I figured it was accurate, but not with big numbers like years.
     
    Last edited: Jan 20, 2017
  13. http://stackoverflow.com/a/15461376/4011614

    Then create a StringBuilder and do this for every time unit (variable):
    Code:java
    1. if (year != 0) sb.append(year).append(" year(s), ");
    2. if (month != 0) sb.append(month).append(" month(s), ");
    3. // days, hours, minutes, seconds

    If done with appending, convert it to a String, Check if it ends with ", " and remove that with substring
     
    Last edited: Jan 20, 2017
  14. Offline

    JanTuck

    @FisheyLP

    If it returns negative value he should check if its >0
     
  15. Offline

    DoggyCode™

    It doesn't. And my method inevitably works, but the moment it gets to weeks, months and years, the calculations gets messed up
     
  16. could be that calendar months go from 0...11 in java and years from 1970 maybe, you need to add a certain value to those
     
  17. Offline

    DoggyCode™

    What would the value be?

    from 0...11, I'm gonna try, thanks.

    EDIT:
    Tried, still wrong output. About 1 month ahead
    Code:
    long x = millis / 1000;
            long seconds = x % 60;
    
            x /= 60;
            long minutes = x % 60;
    
            x /= 60;
            long hours = x % 24;
    
            x /= 24;
            long days = x % 7;
    
            x /= 7;
            long weeks = x % 4;
    
            x /= 4;
            long months = x;
    
            StringBuilder sb = new StringBuilder();
            if(months>0) {
                sb.append(months).append(" months").append(", ");
            }
            if(weeks>0) {
                sb.append(weeks).append(" weeks").append(", ");
            }
            if(days>0) {
                sb.append(days).append(" days").append(", ");
            }
            if(hours>0) {
                sb.append(hours).append(" hours").append(", ");
            }
            if(minutes>0) {
                sb.append(minutes).append(" minutes").append(", ");
            }
            if(seconds>0) {
                sb.append(seconds).append(" seconds").append(", ");
            }
    
            String tmp = sb.toString().trim();
            if(tmp.endsWith(",")) {
                tmp = tmp.substring(0, tmp.length()-1);
            }
     
    Last edited: Jan 20, 2017
Thread Status:
Not open for further replies.

Share This Page