Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get GMT date from Unix epoch time milliseconds?

I trying convert unix milliseconds to gmt date, I need only hours and minutes, but results are incorrect 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
like image 761
Dennis Zinkovski Avatar asked Dec 20 '25 20:12

Dennis Zinkovski


2 Answers

Change calendar.setTimeInMillis(time / 1000) to calendar.setTimeInMillis(time * 1000)

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

like image 140
Dawood ibn Kareem Avatar answered Dec 23 '25 14:12

Dawood ibn Kareem


 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 keep seconds optional here

like image 44
Syed Atir Mohiuddin Avatar answered Dec 23 '25 13:12

Syed Atir Mohiuddin