milliseconds to days

前端 未结 5 831
情深已故
情深已故 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:01

    If you don't have another time interval bigger than days:

    int days = (int) (milliseconds / (1000*60*60*24));
    

    If you have weeks too:

    int days = (int) ((milliseconds / (1000*60*60*24)) % 7);
    int weeks = (int) (milliseconds / (1000*60*60*24*7));
    

    It's probably best to avoid using months and years if possible, as they don't have a well-defined fixed length. Strictly speaking neither do days: daylight saving means that days can have a length that is not 24 hours.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-13 04:17
    public static final long SECOND_IN_MILLIS = 1000;
    public static final long MINUTE_IN_MILLIS = SECOND_IN_MILLIS * 60;
    public static final long HOUR_IN_MILLIS = MINUTE_IN_MILLIS * 60;
    public static final long DAY_IN_MILLIS = HOUR_IN_MILLIS * 24;
    public static final long WEEK_IN_MILLIS = DAY_IN_MILLIS * 7;
    

    You could cast int but I would recommend using long.

    0 讨论(0)
  • 2020-12-13 04:22

    For simple cases like this, TimeUnit should be used. TimeUnit usage is a bit more explicit about what is being represented and is also much easier to read and write when compared to doing all of the arithmetic calculations explicitly. For example, to calculate the number days from milliseconds, the following statement would work:

        long days = TimeUnit.MILLISECONDS.toDays(milliseconds);
    

    For cases more advanced, where more finely grained durations need to be represented in the context of working with time, an all encompassing and modern date/time API should be used. For JDK8+, java.time is now included (here are the tutorials and javadocs). For earlier versions of Java joda-time is a solid alternative.

    0 讨论(0)
  • 2020-12-13 04:22
    int days = (int) (milliseconds / 86 400 000 )
    
    0 讨论(0)
提交回复
热议问题