Java TimeZone Format formatTimeZoneOffset(TimeZone timeZone)

Here you can find the source of formatTimeZoneOffset(TimeZone timeZone)

Description

Returns the formatted offset of the specified time zone at the current local time.

License

MIT License

Parameter

Parameter Description
timeZone a time zone.

Return

the formatted offset (e.g. UTC+01:00).

Declaration

public static String formatTimeZoneOffset(TimeZone timeZone) 

Method Source Code

//package com.java2s;
/*/* w  w  w.j a v a 2  s . c o  m*/
 * Copyright (c) 2015-2016 QuartzDesk.com.
 * Licensed under the MIT license (https://opensource.org/licenses/MIT).
 */

import java.util.TimeZone;

public class Main {
    /**
     * Returns the formatted offset of the specified time zone at the current local time.
     *
     * @param timeZone a time zone.
     * @return the formatted offset (e.g. UTC+01:00).
     */
    public static String formatTimeZoneOffset(TimeZone timeZone) {
        int offsetSeconds = timeZone.getOffset(System.currentTimeMillis()) / 1000;

        int offsetHours = Math.abs(offsetSeconds / 3600);
        int offsetMinutes = (Math.abs(offsetSeconds) - (offsetHours * 3600)) / 60;

        StringBuilder offset = new StringBuilder("UTC");
        if (offsetSeconds >= 0) {
            offset.append('+');
        } else {
            offset.append('-');
        }

        if (offsetHours < 10)
            offset.append('0');

        offset.append(offsetHours);

        offset.append(':');

        if (offsetMinutes < 10)
            offset.append('0');

        offset.append(offsetMinutes);

        return offset.toString();
    }
}

Related

  1. createDateFormat(String pattern, TimeZone timeZone)
  2. createDateFormat(TimeZone timezone)
  3. createDateFormatsForLocaleAndTimeZone(Locale locale, TimeZone timeZone)
  4. formatISO8601TimeZone(TimeZone timeZone, boolean extended)
  5. formatOffset(TimeZone zone)
  6. getCalendar(String dateString, String dateTimeFormat, String timeZoneName)
  7. getCalendar(String dateString, String format, TimeZone tz)
  8. getDefaultDateFormatWithoutTimeZone( TimeZone zone)
  9. getDefaultDateFormatWithTimeZone()