How to get GMT date from Unix epoch of millisecond time?

I am trying to convert unix milliseconds to gmt date, I only need hours and minutes, but the results are not correct according to online converters.

What I need

enter image description here

Here is my code:

 public static void main(String[] args) {
    long time = 1438050023;
   // TimeZone timeZone = TimeZone.getTimeZone("UTC");
    Calendar calendar = Calendar.getInstance();

    calendar.setTimeInMillis(time / 1000);

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm:ss dd MM yyyy");
    simpleDateFormat.setTimeZone(calendar.getTimeZone());

    System.out.println(simpleDateFormat.format(calendar.getTime()));
}

      

Result:

03:23:58 01 01 1970

      

+3


source to share


2 answers


Change calendar.setTimeInMillis(time / 1000)

tocalendar.setTimeInMillis(time * 1000)



The number of milliseconds is 1000 times the number of seconds; not 1/1000 numbers.

+4


source


 public static String ConvertMillistoDatetime(long millis) {
    long second = (millis / 1000) % 60;
    long minute = (millis / (1000 * 60)) % 60;
    long hour = (millis / (1000 * 60 * 60)) % 24;

    return String.format("%02d:%02d:%02d", hour, minute, second);
}

      



Try this, you can save seconds here

+1


source







All Articles