Java TimeZone Format formatOffset(TimeZone zone)

Here you can find the source of formatOffset(TimeZone zone)

Description

Produce a string describing the offset of the given time zone from UTC, including the DST offset if there is one.

License

Open Source License

Parameter

Parameter Description
zone TimeZone whose offset we want.

Return

Formatted offset.

Declaration

public static String formatOffset(TimeZone zone) 

Method Source Code

//package com.java2s;
/**//from   w  ww.  ja  v a 2s  .c o  m
 * utils: general utility functions.
 * <br>Copyright 2004-2009 Ian Cameron Smith
 *
 * <p>This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation (see COPYING).
 *
 * <p>This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * <p>You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */

import java.util.TimeZone;

public class Main {
    /**
     * Produce a string describing the offset of the given time zone from
     * UTC, including the DST offset if there is one.
     * 
     * @param   zone         TimeZone whose offset we want.
     * @return               Formatted offset.
     */
    public static String formatOffset(TimeZone zone) {
        String fmt = "";

        int base = zone.getRawOffset();
        fmt += "UTC" + intervalMsToHmsShort(base);

        int dst = zone.getDSTSavings();
        if (dst != 0)
            fmt += " (UTC" + intervalMsToHmsShort(base + dst) + ")";

        return fmt;
    }

    /**
     * Format a time offset as hours, minutes and seconds, only including
     * the relevant components.  If the offset is less than 60 sec, only
     * the seconds will be included; if the offset is an exact number of
     * hours, only hours; etc.
     *
     * @param   off         The time offset in ms to format.
     * @return            The formatted value.
     */
    public static String intervalMsToHmsShort(long off) {
        if (off == 0)
            return "";

        String hms = "";

        off /= 1000;
        if (off >= 0) {
            hms += "+";
        } else {
            hms += "-";
            off = -off;
        }

        if (off >= 3600) {
            hms += (off / 3600) + "h";
            off = off % 3600;
        }
        if (off >= 60) {
            hms += (off / 60) + "m";
            off = off % 60;
        }
        if (off > 0)
            hms += off + "s";

        return hms;
    }
}

Related

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