datetime – How to convert timestamp to readable time without considering time zones and daylight saving time?

Question:

There is a method for converting UTC time to a readable format.

public static String convertUTC(final long time)
{
        final Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
        calendar.setTimeInMillis(time);
        final SimpleDateFormat format = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
        return format.format(calendar.getTime());
}

But the problem is that the time is obtained taking into account the transition to summer and winter time, and the time zone on the device is also taken into account

For example, if you call

convertUTC(1397676512436)

then it will return 2014: 04: 17 01:28:32 . At this moment, the time zone is GMT + 6 on the device

Answer:

The problem is solved, it was necessary to add

format.setTimeZone(TimeZone.getTimeZone("UTC"));
Scroll to Top