Java TimeZone Format formatISO8601TimeZone(TimeZone timeZone, boolean extended)

Here you can find the source of formatISO8601TimeZone(TimeZone timeZone, boolean extended)

Description

Converts a TimeZone object to a string that's in ISO-8601 format.

License

Open Source License

Parameter

Parameter Description

Return

the formatted timezone (e.g. "+0530" or "+05:30")

Declaration

public static String formatISO8601TimeZone(TimeZone timeZone, boolean extended) 

Method Source Code

//package com.java2s;

import java.util.TimeZone;

public class Main {
    /**//from w  w w. j  a v  a  2s. c o m
     * <p>
     * Converts a TimeZone object to a string that's in ISO-8601 format. It can
     * be either basic or extended format.
     * </p>
     * 
     * @param - timeZone the timezone to format
     * @param - extended true to use "extended" format, false not to. Extended
     *  format will put a colon between the hour and minute.
     * @return the formatted timezone (e.g. "+0530" or "+05:30")
     */
    public static String formatISO8601TimeZone(TimeZone timeZone, boolean extended) {
        StringBuilder sb = new StringBuilder();
        boolean positive = timeZone.getRawOffset() >= 0;
        int hours = Math.abs(((timeZone.getRawOffset() / 1000) / 60) / 60);
        int minutes = Math.abs((timeZone.getRawOffset() / 1000) / 60) % 60;

        sb.append(positive ? '+' : '-');

        if (hours < 10) {
            sb.append('0');
        }
        sb.append(hours);

        if (extended) {
            sb.append(':');
        }

        if (minutes < 10) {
            sb.append('0');
        }
        sb.append(minutes);

        return sb.toString();
    }
}

Related

  1. addDateFormatsToList(Locale locale, TimeZone timeZone, List formats)
  2. createDateFormat(String format, TimeZone tz)
  3. createDateFormat(String pattern, TimeZone timeZone)
  4. createDateFormat(TimeZone timezone)
  5. createDateFormatsForLocaleAndTimeZone(Locale locale, TimeZone timeZone)
  6. formatOffset(TimeZone zone)
  7. formatTimeZoneOffset(TimeZone timeZone)
  8. getCalendar(String dateString, String dateTimeFormat, String timeZoneName)
  9. getCalendar(String dateString, String format, TimeZone tz)