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

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
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With