Convert timestamp long to normal date format

前端 未结 5 1451
南旧
南旧 2020-12-09 03:46

In my web app, date & time of a user\'s certain activity is stored(in database) as a timestamp Long which on being displayed back to user needs to be conver

5条回答
  •  醉话见心
    2020-12-09 04:28

    To show leading zeros infront of hours, minutes and seconds use below modified code. The trick here is we are converting (or more accurately formatting) integer into string so that it shows leading zero whenever applicable :

    public String convertTimeWithTimeZome(long time) {
            Calendar cal = Calendar.getInstance();
            cal.setTimeZone(TimeZone.getTimeZone("UTC"));
            cal.setTimeInMillis(time);
            String curTime = String.format("%02d:%02d:%02d", cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND));
            return curTime;
        }
    

    Result would be like : 00:01:30

提交回复
热议问题