TimeZone with Calendar confusing results

前端 未结 5 2100
情深已故
情深已故 2021-01-06 06:19

I have been working with timezone conversions lately and am quite astonished by the result i get.Basically, i want to convert a date from one timezone into another. below is

5条回答
  •  遥遥无期
    2021-01-06 06:57

    This code works for me to convert to UTC:

    1. create a Calendar Object with UTC time zone

      Calendar utcTime = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
      
    2. set the point in time in the UTC Calendar object from the "any time zone" Calendar object

      utcTime.setTimeInMillis(myCalendarObjectInSomeOtherTimeZone.getTimeInMillis());
      
    3. utcTime will now contain the same point in time as myCalendarObjectInSomeOtherTimeZone converted to UTC.

提交回复
热议问题