milliseconds to days

前端 未结 5 830
情深已故
情深已故 2020-12-13 03:58

i did some research, but still can\'t find how to get the days... Here is what I got:

int seconds = (int) (milliseconds / 1000) % 60 ;
int minutes = (int) ((         


        
5条回答
  •  醉梦人生
    2020-12-13 04:12

    In case you solve a more complex task of logging execution statistics in your code:

    public void logExecutionMillis(LocalDateTime start, String callerMethodName) {
    
      LocalDateTime end = getNow();
      long difference = Duration.between(start, end).toMillis();
    
      Logger logger = LoggerFactory.getLogger(ProfilerInterceptor.class);
    
      long millisInDay = 1000 * 60 * 60 * 24;
      long millisInHour = 1000 * 60 * 60;
      long millisInMinute = 1000 * 60;
      long millisInSecond = 1000;
    
      long days = difference / millisInDay;
      long daysDivisionResidueMillis = difference - days * millisInDay;
    
      long hours = daysDivisionResidueMillis / millisInHour;
      long hoursDivisionResidueMillis = daysDivisionResidueMillis - hours * millisInHour;
    
      long minutes = hoursDivisionResidueMillis / millisInMinute;
      long minutesDivisionResidueMillis = hoursDivisionResidueMillis - minutes * millisInMinute;
    
      long seconds = minutesDivisionResidueMillis / millisInSecond;
      long secondsDivisionResidueMillis = minutesDivisionResidueMillis - seconds * millisInSecond;
    
      logger.info(
          "\n************************************************************************\n"
              + callerMethodName
              + "() - "
              + difference
              + " millis ("
              + days
              + " d. "
              + hours
              + " h. "
              + minutes
              + " min. "
              + seconds
              + " sec."
              + secondsDivisionResidueMillis
              + " millis).");
    }
    

    P.S. Logger can be replaced with simple System.out.println() if you like.

提交回复
热议问题