Example usage for java.util TimeZone getID

List of usage examples for java.util TimeZone getID

Introduction

In this page you can find the example usage for java.util TimeZone getID.

Prototype

public String getID() 

Source Link

Document

Gets the ID of this time zone.

Usage

From source file:lucee.runtime.converter.JSONDateFormat.java

public synchronized static String format(Date date, TimeZone tz) {
    tz = ThreadLocalPageContext.getTimeZone(tz);
    String id = locale.hashCode() + "-" + tz.getID();
    DateFormat format = (DateFormat) map.get(id);
    if (format == null) {
        format = new SimpleDateFormat("MMMM, dd yyyy HH:mm:ss Z", locale);
        format.setTimeZone(tz);//from   ww w.j a v  a  2 s .c  o  m
        map.put(id, format);
    }

    return format.format(date);
}

From source file:org.hippoecm.frontend.plugins.standards.datetime.DateTimePrinter.java

static ZoneId toZoneId(final TimeZone timeZone) {
    return ZoneId.of(timeZone.getID(), ZoneId.SHORT_IDS);
}

From source file:Main.java

/**
 * Parse a date from ISO-8601 formatted string. It expects a format yyyy-MM-ddThh:mm:ss[.sss][Z|[+-]hh:mm]
 *
 * @param date ISO string to parse in the appropriate format.
 * @return the parsed date/*from   www  .  j a v  a2s  . com*/
 * @throws IllegalArgumentException if the date is not in the appropriate format
 */
public static Date parse(String date) {
    try {
        int offset = 0;

        // extract year
        int year = parseInt(date, offset, offset += 4);
        checkOffset(date, offset, '-');

        // extract month
        int month = parseInt(date, offset += 1, offset += 2);
        checkOffset(date, offset, '-');

        // extract day
        int day = parseInt(date, offset += 1, offset += 2);
        checkOffset(date, offset, 'T');

        // extract hours, minutes, seconds and milliseconds
        int hour = parseInt(date, offset += 1, offset += 2);
        checkOffset(date, offset, ':');

        int minutes = parseInt(date, offset += 1, offset += 2);
        checkOffset(date, offset, ':');

        int seconds = parseInt(date, offset += 1, offset += 2);
        // milliseconds can be optional in the format
        int milliseconds = 0; // always use 0 otherwise returned date will include millis of current time
        if (date.charAt(offset) == '.') {
            checkOffset(date, offset, '.');
            milliseconds = parseInt(date, offset += 1, offset += 3);
        }

        // extract timezone
        String timezoneId;
        char timezoneIndicator = date.charAt(offset);
        if (timezoneIndicator == '+' || timezoneIndicator == '-') {
            timezoneId = GMT_ID + date.substring(offset);
        } else if (timezoneIndicator == 'Z') {
            timezoneId = GMT_ID;
        } else {
            throw new IndexOutOfBoundsException("Invalid time zone indicator " + timezoneIndicator);
        }
        TimeZone timezone = TimeZone.getTimeZone(timezoneId);
        if (!timezone.getID().equals(timezoneId)) {
            throw new IndexOutOfBoundsException();
        }

        Calendar calendar = new GregorianCalendar(timezone);
        calendar.setLenient(false);
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, day);
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minutes);
        calendar.set(Calendar.SECOND, seconds);
        calendar.set(Calendar.MILLISECOND, milliseconds);

        return calendar.getTime();
    } catch (IndexOutOfBoundsException e) {
        throw new IllegalArgumentException("Failed to parse date " + date, e);
    } catch (NumberFormatException e) {
        throw new IllegalArgumentException("Failed to parse date " + date, e);
    } catch (IllegalArgumentException e) {
        throw new IllegalArgumentException("Failed to parse date " + date, e);
    }
}

From source file:org.eclipse.hawkbit.ui.utils.SPDateTimeUtil.java

/**
 * Get time zone id .ZoneId.SHORT_IDS used get id if time zone is
 * abbreviated like 'IST'./*  w  ww .  j  a v a 2s.  co m*/
 *
 * @param tz
 * @return ZoneId
 */
public static ZoneId getTimeZoneId(final TimeZone tz) {
    return ZoneId.of(tz.getID(), ZoneId.SHORT_IDS);
}

From source file:at.bitfire.davdroid.resource.iCalendar.java

/**
 * Ensures that a given DateProperty has a time zone with an ID that is available in Android.
 * @param date   DateProperty to validate. Values which are not DATE-TIME will be ignored.
 *///from  w w  w  . ja va2  s .  co m
protected static void validateTimeZone(DateProperty date) {
    if (isDateTime(date)) {
        final TimeZone tz = date.getTimeZone();
        if (tz == null)
            return;
        final String tzID = tz.getID();
        if (tzID == null)
            return;

        String deviceTzID = DateUtils.findAndroidTimezoneID(tzID);
        if (!tzID.equals(deviceTzID))
            date.setTimeZone(DateUtils.tzRegistry.getTimeZone(deviceTzID));
    }
}

From source file:Main.java

/**
 * Parse a date from ISO-8601 formatted string. It expects a format
 * yyyy-MM-ddThh:mm:ss[.sss][Z|[+-]hh:mm]
 *
 * @param date ISO string to parse in the appropriate format.
 * @return the parsed date//  w  w w  . j  a  va2  s.  c om
 * @throws IllegalArgumentException if the date is not in the appropriate format
 */
public static Date parse(String date) {
    try {
        int offset = 0;

        // extract year
        int year = parseInt(date, offset, offset += 4);
        checkOffset(date, offset, '-');

        // extract month
        int month = parseInt(date, offset += 1, offset += 2);
        checkOffset(date, offset, '-');

        // extract day
        int day = parseInt(date, offset += 1, offset += 2);
        checkOffset(date, offset, 'T');

        // extract hours, minutes, seconds and milliseconds
        int hour = parseInt(date, offset += 1, offset += 2);
        checkOffset(date, offset, ':');

        int minutes = parseInt(date, offset += 1, offset += 2);
        checkOffset(date, offset, ':');

        int seconds = parseInt(date, offset += 1, offset += 2);
        // milliseconds can be optional in the format
        // always use 0 otherwise returned date will include millis of current time
        int milliseconds = 0;

        if (date.charAt(offset) == '.') {
            checkOffset(date, offset, '.');
            int digitCount = 1;
            while (offset + digitCount < date.length() && digitCount < 3
                    && date.charAt(offset + 1 + digitCount) != 'Z'
                    && date.charAt(offset + 1 + digitCount) != '+'
                    && date.charAt(offset + 1 + digitCount) != '-') {
                digitCount++;
            }
            String msString = date.substring(offset += 1, offset += digitCount);
            while (msString.length() < 3) {
                msString += '0';
            }
            milliseconds = parseInt(msString, 0, 3);
        }

        // extract timezone
        String timezoneId = null;
        while (offset < date.length()) {
            char timezoneIndicator = date.charAt(offset);
            if (timezoneIndicator == '+' || timezoneIndicator == '-') {
                timezoneId = GMT_ID + date.substring(offset);
                break;
            } else if (timezoneIndicator == 'Z') {
                timezoneId = GMT_ID;
                break;
            }
            offset++;
        }
        if (timezoneId == null) {
            throw new IndexOutOfBoundsException("Invalid time zone indicator ");
        }
        TimeZone timezone = TimeZone.getTimeZone(timezoneId);
        if (!timezone.getID().equals(timezoneId)) {
            throw new IndexOutOfBoundsException();
        }

        Calendar calendar = new GregorianCalendar(timezone);
        calendar.setLenient(false);
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, day);
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minutes);
        calendar.set(Calendar.SECOND, seconds);
        calendar.set(Calendar.MILLISECOND, milliseconds);

        return calendar.getTime();
    } catch (IndexOutOfBoundsException | IllegalArgumentException e) {
        throw new IllegalArgumentException("Failed to parse date " + date, e);
    }
}

From source file:Main.java

public static String getCurrentTimeZoneID() {
    //String timezonePropertyValue = SecurityActions.getSystemProperty(GeneralConstants.TIMEZONE, "GMT");

    TimeZone timezone;
    //if (GeneralConstants.TIMEZONE_DEFAULT.equals(timezonePropertyValue)) {
    timezone = TimeZone.getDefault();
    //} else {/*from   ww w  .  j  av a  2 s . co  m*/
    //   timezone = TimeZone.getTimeZone(timezonePropertyValue);
    //}

    System.out.println("TIMEZONE:" + timezone.getID());
    return timezone.getID();
}

From source file:lucee.commons.i18n.FormatUtil.java

public static DateFormat[] getDateFormats(Locale locale, TimeZone tz, boolean lenient) {
    String id = "d-" + locale.hashCode() + "-" + tz.getID() + "-" + lenient;
    DateFormat[] df = formats.get(id);
    if (df == null) {
        List<DateFormat> list = new ArrayList<DateFormat>();
        list.add(DateFormat.getDateInstance(DateFormat.FULL, locale));
        list.add(DateFormat.getDateInstance(DateFormat.LONG, locale));
        list.add(DateFormat.getDateInstance(DateFormat.MEDIUM, locale));
        list.add(DateFormat.getDateInstance(DateFormat.SHORT, locale));
        addCustom(list, locale, FORMAT_TYPE_DATE);
        df = list.toArray(new DateFormat[list.size()]);

        for (int i = 0; i < df.length; i++) {
            df[i].setLenient(lenient);//  ww  w  .  j a v  a2 s  . c om
            df[i].setTimeZone(tz);
        }
        formats.put(id, df);
    }
    return df;
}

From source file:lucee.commons.i18n.FormatUtil.java

public static DateFormat[] getTimeFormats(Locale locale, TimeZone tz, boolean lenient) {
    String id = "t-" + locale.hashCode() + "-" + tz.getID() + "-" + lenient;
    DateFormat[] df = formats.get(id);
    if (df == null) {
        List<DateFormat> list = new ArrayList<DateFormat>();
        list.add(DateFormat.getTimeInstance(DateFormat.FULL, locale));
        list.add(DateFormat.getTimeInstance(DateFormat.LONG, locale));
        list.add(DateFormat.getTimeInstance(DateFormat.MEDIUM, locale));
        list.add(DateFormat.getTimeInstance(DateFormat.SHORT, locale));
        add24(list, locale);//from  w ww . jav  a2 s  . c  o  m
        addCustom(list, locale, FORMAT_TYPE_TIME);
        df = list.toArray(new DateFormat[list.size()]);

        for (int i = 0; i < df.length; i++) {
            df[i].setLenient(lenient);
            df[i].setTimeZone(tz);
        }
        formats.put(id, df);
    }
    return df;
}

From source file:lucee.commons.i18n.FormatUtil.java

public static DateFormat[] getDateTimeFormats(Locale locale, TimeZone tz, boolean lenient) {

    String id = "dt-" + locale.hashCode() + "-" + tz.getID() + "-" + lenient;
    DateFormat[] df = formats.get(id);
    if (df == null) {
        List<DateFormat> list = new ArrayList<DateFormat>();
        list.add(DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, locale));
        list.add(DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.LONG, locale));
        list.add(DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.MEDIUM, locale));
        list.add(DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.SHORT, locale));

        list.add(DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.FULL, locale));
        list.add(DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale));
        list.add(DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.MEDIUM, locale));
        list.add(DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT, locale));

        list.add(DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.FULL, locale));
        list.add(DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG, locale));
        list.add(DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, locale));
        list.add(DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, locale));

        list.add(DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.FULL, locale));
        list.add(DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG, locale));
        list.add(DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, locale));
        list.add(DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, locale));
        add24(list, locale);/* www. jav a 2  s  .  c  o m*/
        addCustom(list, locale, FORMAT_TYPE_DATE_TIME);
        df = list.toArray(new DateFormat[list.size()]);

        for (int i = 0; i < df.length; i++) {
            df[i].setLenient(lenient);
            df[i].setTimeZone(tz);
        }

        formats.put(id, df);
    }

    return df;
}