java - Converting time to "HH:mm:ss" using Calendar and SimpleDateFormat adds 1 hour -


request

I need to change "HH: mm: ss" - the time saved in seconds I (and other formats in the future).

Example 9 seconds -> "00:00:09" . However, calendar class always adds +1 hours I think it's my timezone (which is "Europe / Prague ") or daylight saving time.

test

date class. Then with three different timezones of calendar , the methods setTimeInMillis () and set () .

  // Announcement calendar calendar; SimpleDateFormat simpleDateFormat = New SimpleDateFormat (format); String result;  

Use of date range:

  // Simple date range access date date = new date (TimeIcek * 1000); Results = Simple Data Format (date); // Invalid result: "01:00:09"  

Calendar class with "GMT":

  // calendar - time zone GMT cal = New Gregorian Calendar (Timezone. Get Time Zone ("GMT")); Cal.setTimeInMillis (timeInSec * 1000); Results = Simple Data Format Format (cal.getTime ()); // False result: "01:00:09" Cal .set (1970, Calendar. Januri, 1, 0, 0, Time Insec); Results = Simple Data Format Format (cal.getTime ()); // Invalid result: "01:00:09"  

Calendar class with "UTC":

  // calendar - timezone UTC cal = new Gregorian Calendar (Timezone. Get Time Zone ("UTC")); Cal.setTimeInMillis (timeInSec * 1000); Results = Simple Data Format Format (cal.getTime ()); // False result: "01:00:09" Cal .set (1970, Calendar. Januri, 1, 0, 0, Time Insec); Results = Simple Data Format Format (cal.getTime ()); Calendar class with "wrong" - "Europe / Prague":  
  // calendar - time "default" (this sets "Europe / Prague") time = New Gregorian calendar (Timezone .gate default ()); Cal.setTimeInMillis (timeInSec * 1000); Results = Simple Data Format Format (cal.getTime ()); // False result: "01:00:09" Cal .set (1970, Calendar. Januri, 1, 0, 0, Time Insec); Results = Simple Data Format Format (cal.getTime ()); // Correct results: "00:00:09"  

In the last case I got the right result but I can not understand.

question

  1. Why does the last situation work? (And not one before that?)
  2. How do I use Calendar? Could the square be able to pass just in seconds (without parsing)?
  3. Is there any other solution (second class)? Exclude it to parse itself. calendar for

    timezone (last) Do not agree to and SimpleDateFormat ( excluding ). You can use the same time zone to set and it should work.

      // calendar - timezone UTC cal = new Gregorian calendar (Timezone.Gate Time Zone ("UTC")); Cal.setTimeInMillis (timeInSec * 1000); SimpleDateFormat.setTimeZone (Timezone. Get Time Zone ("UTC")); // & lt; - This is the result = Simple data format. Format (cal.getTime ()); Cal.set (1970, calendar January, 1, 0, 0, time incec); Results = Simple Data Format Format (cal.getTime ());  

Comments