Wednesday, February 15, 2017

Java : convert milliseconds hours, minutes, seconds ,days

An example using built in  TimeUnit .
                            long uptime = System.currentTimeMillis();

                            long days = TimeUnit.MILLISECONDS
                                    .toDays(uptime);
                            uptime -= TimeUnit.DAYS.toMillis(days);

                            long hours = TimeUnit.MILLISECONDS
                                    .toHours(uptime);
                            uptime -= TimeUnit.HOURS.toMillis(hours);

                            long minutes = TimeUnit.MILLISECONDS
                                    .toMinutes(uptime);
                            uptime -= TimeUnit.MINUTES.toMillis(minutes);

                            long seconds = TimeUnit.MILLISECONDS
                                    .toSeconds(uptime);

uptime:22hr 15min 40sec 14 days


No comments :

Post a Comment