Example usage for java.util TimeZone hasSameRules

List of usage examples for java.util TimeZone hasSameRules

Introduction

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

Prototype

public boolean hasSameRules(TimeZone other) 

Source Link

Document

Returns true if this zone has the same rule and offset as another zone.

Usage

From source file:Main.java

public static void main(String args[]) {

    TimeZone timezoneone = TimeZone.getDefault();
    TimeZone timezonetwo = TimeZone.getDefault();

    // comparing two time zones
    boolean res = timezoneone.hasSameRules(timezonetwo);

    // checking the result   
    System.out.println("Comparison result:" + res);
}

From source file:com.microsoft.exchange.DateHelpTest.java

@Test
public void systemTimeZone() {
    List<String> availableIDs = Arrays.asList(TimeZone.getAvailableIDs());
    assertFalse(CollectionUtils.isEmpty(availableIDs));
    assertTrue(availableIDs.contains(TimeZones.UTC_ID));
    assertTrue(availableIDs.contains("UTC"));

    TimeZone ical4jUTC = TimeZone.getTimeZone(TimeZones.UTC_ID);
    TimeZone sysUTC = TimeZone.getTimeZone("UTC");

    assertEquals(ical4jUTC.getDSTSavings(), sysUTC.getDSTSavings());
    assertEquals(ical4jUTC.getRawOffset(), sysUTC.getRawOffset());
    assertTrue(ical4jUTC.hasSameRules(sysUTC));

    TimeZone origDefaultTimeZone = TimeZone.getDefault();
    assertNotNull(origDefaultTimeZone);/*from   w ww . j  a v a 2  s  .c  o m*/
    assertEquals(TimeZone.getDefault().getRawOffset(), origDefaultTimeZone.getRawOffset());

    log.info("TimeZone.DisplayName=" + origDefaultTimeZone.getDisplayName());
    log.info("TimeZone.ID=" + origDefaultTimeZone.getID());
    log.info("TimeZone.DSTSavings=" + origDefaultTimeZone.getDSTSavings());
    log.info("TimeZone.RawOffset=" + origDefaultTimeZone.getRawOffset());
    log.info("TimeZone.useDaylightTime=" + origDefaultTimeZone.useDaylightTime());

    TimeZone.setDefault(ical4jUTC);
    assertEquals(ical4jUTC, TimeZone.getDefault());
    log.info(" -- Defualt Time Zone has been changed successfully! -- ");

    TimeZone newDefaultTimeZone = TimeZone.getDefault();
    log.info("TimeZone.DisplayName=" + newDefaultTimeZone.getDisplayName());
    log.info("TimeZone.ID=" + newDefaultTimeZone.getID());
    log.info("TimeZone.DSTSavings=" + newDefaultTimeZone.getDSTSavings());
    log.info("TimeZone.RawOffset=" + newDefaultTimeZone.getRawOffset());
    log.info("TimeZone.useDaylightTime=" + newDefaultTimeZone.useDaylightTime());
}

From source file:com.jaspersoft.jasperserver.war.common.JdkTimeZonesList.java

protected TimeZone findExistingDefaultTZ() {
    TimeZone defaultTz = getSystemDefault();
    TimeZone existingTz = null;// www  .j  ava  2 s  .  c  o  m
    for (Iterator it = timeZonesIds.iterator(); it.hasNext();) {
        String id = (String) it.next();
        if (id.equals(defaultTz.getID())) {//exact match
            existingTz = defaultTz;
            break;
        } else if (existingTz == null) {
            TimeZone timeZone = loadTimeZone(id);
            if (defaultTz.hasSameRules(timeZone)) {
                existingTz = timeZone;
            }
        }
    }
    return existingTz;
}

From source file:org.apache.openmeetings.web.app.WebSession.java

public String getClientTZCode() {
    TimeZone _zone = browserTz;/*from w  ww.j  av  a  2  s  .c o m*/
    if (browserTz == null) {
        try {
            browserTz = getClientInfo().getProperties().getTimeZone();
            if (browserTz != null && !AVAILABLE_TIMEZONE_SET.contains(browserTz.getID())) {
                for (String availableID : AVAILABLE_TIMEZONES) {
                    TimeZone zone = TimeZone.getTimeZone(availableID);
                    if (zone.hasSameRules(browserTz)) {
                        browserTz = zone;
                        break;
                    }
                }
            }
            _zone = browserTz;
        } catch (Exception e) {
            //no-op
        }
        if (browserTz == null) {
            _zone = Calendar.getInstance(getBrowserLocale()).getTimeZone();
        }
    }
    return _zone == null ? null : _zone.getID();
}

From source file:com.microsoft.exchange.DateHelpTest.java

/**
 * Computes the time zone offset for a given {@link XMLGregorianCalendar} and compares to the specified {@link TimeZone}
 * /*from  www  .  ja  v  a2 s. c om*/
 * 
 * 
 * @param xmlGregorianCalendar
 * @param timeZone
 */
public boolean xmlGregorianCalendareMatchesTimeZone(XMLGregorianCalendar xmlGregorianCalendar,
        TimeZone timeZone) {
    int xmlTimeZoneOffsetMinutes = xmlGregorianCalendar.getTimezone();
    TimeZone xmlTimeZone = xmlGregorianCalendar.getTimeZone(xmlTimeZoneOffsetMinutes);
    int jvmRawOffsetMinutes = (timeZone.getRawOffset() / 1000 / 60);
    int jvmDstOffsetMinutes = (timeZone.getDSTSavings() / 1000 / 60);

    int xmlRawOffsetMinutes = (xmlTimeZone.getRawOffset() / 1000 / 60);
    int xmlDstOffsetMinutes = (xmlTimeZone.getDSTSavings() / 1000 / 60);

    //XMLGregorianCalendar only stores an Int for offset, no DST information.  
    //as a result the xmlTimeZone and jvmTimeZone almost never follow the same rules
    if (timeZone.hasSameRules(xmlTimeZone)) {
        log.debug("xmlTimeZoneId=" + xmlTimeZone.getID() + " hasSameRules as jvmTimeZone=" + timeZone.getID());
        return true;
    }

    if (timeZone.useDaylightTime()) {
        //they definately do not when the jvmTimeZone uses DST.
        assertFalse(xmlTimeZone.hasSameRules(timeZone));
        jvmRawOffsetMinutes += jvmDstOffsetMinutes;
    }

    if (xmlTimeZone.useDaylightTime()) {
        xmlRawOffsetMinutes += xmlDstOffsetMinutes;
    }
    if (xmlTimeZoneOffsetMinutes != xmlRawOffsetMinutes) {
        log.info("xmlTimeZoneId=" + xmlTimeZone.getID() + " has weird rules");
    }

    return (jvmRawOffsetMinutes == xmlRawOffsetMinutes);

}

From source file:com.rareventure.gps2.reviewer.map.OsmMapGpsTrailerReviewerMapActivity.java

private void initSasPanel() {
    ListView sasPanelList = (ListView) findViewById(R.id.sas_grid);

    sasPanelList.setOnItemClickListener(new OnItemClickListener() {

        private static final int MIN_TR_TIMESPAN_SEC = Util.SECONDS_IN_DAY;
        private static final int TR_TIMESPAN_MULTIPLIER = 3;

        @Override//ww w  . jav  a  2 s.  c  o m
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //note that getTimeRange reuses the same tr instance, so we have to be careful
            // when we call it twice
            TimeRange tr = gpsTrailerOverlay.sas.getTimeRange(position - 1);

            int currStartSec = tr.startTimeSec;
            int currEndSec = tr.endTimeSec;

            //co: we just want the time inside the box
            //            int timeSpan;
            //            
            //            if(tr.fullRangeEndSec - tr.fullRangeStartSec < MIN_TR_TIMESPAN_SEC / TR_TIMESPAN_MULTIPLIER)
            //            {
            //               timeSpan = MIN_TR_TIMESPAN_SEC;
            //            }
            //            else
            //            {
            //               timeSpan = (tr.fullRangeEndSec - tr.fullRangeStartSec) * TR_TIMESPAN_MULTIPLIER;
            //            }
            //            
            //            int currStartSec = (int) Math.max(- timeSpan /2 + (tr.endTimeSec/2 + tr.startTimeSec/2), getStartTimeMs()/1000);
            //            int currEndSec = (int) Math.min(timeSpan /2 + (tr.endTimeSec/2 + tr.startTimeSec/2), getEndTimeMs()/1000);
            //            
            //            if(position > 0)
            //            {
            //               TimeRange prevTr = gpsTrailerOverlay.sas.getTimeRange(position-1);
            //               currStartSec = Math.max(prevTr.endTimeSec, currStartSec);
            //            }
            //            
            //            if(position < gpsTrailerOverlay.sas.getTimeRangeCount() - 1)
            //            {
            //               TimeRange nextTr = gpsTrailerOverlay.sas.getTimeRange(position+1);
            //               currEndSec = Math.min(nextTr.startTimeSec, currEndSec);
            //            }
            //            
            if (currEndSec - currStartSec < timeView.getMinSelectableTimeSec()) {
                currEndSec = currStartSec + timeView.getMinSelectableTimeSec();
            }

            setStartAndEndTimeSec(currStartSec, currEndSec);
            updateTimeViewTime();

        }
    });

    sasPanelList.setAdapter(new ListAdapter() {

        @Override
        public void unregisterDataSetObserver(DataSetObserver observer) {
            gpsTrailerOverlay.sas.unregisterDataSetObserver(observer);

        }

        @Override
        public void registerDataSetObserver(DataSetObserver observer) {
            gpsTrailerOverlay.sas.registerDataSetObserver(observer);
        }

        @Override
        public boolean isEmpty() {
            return gpsTrailerOverlay.sas.isEmpty();
        }

        @Override
        public boolean hasStableIds() {
            return false; //because we might merge timeviews
        }

        @Override
        public int getViewTypeCount() {
            return 2;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                //               Log.d(GTG.TAG,"SASPanel: Creating new view");
                if (position == 0)
                    convertView = getLayoutInflater().inflate(R.layout.selected_area_info_top_row, null);
                else
                    convertView = getLayoutInflater().inflate(R.layout.selected_area_info_row, null);
            }
            //            else
            //               Log.d(GTG.TAG,"SASPanel: Reusing view");

            if (position == 0) {
                ((TextView) convertView.findViewById(R.id.totalTime))
                        .setText(Util.convertMsToText(gpsTrailerOverlay.sas.getTotalTimeSecs() * 1000l));
                ((TextView) convertView.findViewById(R.id.totalDist)).setText(MapScaleWidget
                        .calcLabelForLength(gpsTrailerOverlay.sas.getTotalDistM(), GTG.prefs.useMetric));
                ((TextView) convertView.findViewById(R.id.timesInArea))
                        .setText(String.valueOf(gpsTrailerOverlay.sas.getTimesInArea()));
                ((TextView) convertView.findViewById(R.id.timesInArea))
                        .setText(String.valueOf(gpsTrailerOverlay.sas.getTimesInArea()));

                TimeZone tz = gpsTrailerOverlay.sas.timeZone;

                if (tz == null || tz.hasSameRules(Util.getCurrTimeZone())) {
                    convertView.findViewById(R.id.timeZoneTableRow).setVisibility(View.GONE);
                } else {
                    convertView.findViewById(R.id.timeZoneTableRow).setVisibility(View.VISIBLE);
                    ((TextView) convertView.findViewById(R.id.timezone)).setText(tz.getDisplayName());
                }

                return convertView;
            }

            TimeRange tr = gpsTrailerOverlay.sas.getTimeRange(position - 1);

            timeAndDateSdf.setTimeZone(gpsTrailerOverlay.sas.timeZone);

            String startText = timeAndDateSdf.format(new Date(tr.startTimeSec * 1000l));
            String endText = timeAndDateSdf.format(new Date(tr.endTimeSec * 1000l));

            ((TextView) convertView.findViewById(R.id.startTime)).setText(startText);
            ((TextView) convertView.findViewById(R.id.endTime)).setText(endText);

            String distText;
            if (tr.dist == -1)
                distText = "--";
            else {
                distText = MapScaleWidget.calcLabelForLength(tr.dist, GTG.prefs.useMetric);
            }

            ((TextView) convertView.findViewById(R.id.distance)).setText(distText);

            //this fixes a bug where sometimes if the last row is deleted and readded, it isn't
            //layedout again, causing the date/times to be cut off
            convertView.requestLayout();

            //            ((TextView)convertView.findViewById(R.id.timeLength)).setText("a certain amount of time");

            return convertView;
        }

        @Override
        public int getItemViewType(int position) {
            if (position == 0)
                return 0;
            return 1;
        }

        @Override
        public long getItemId(int position) {
            if (position == 0)
                return Long.MIN_VALUE;

            TimeRange tr = gpsTrailerOverlay.sas.getTimeRange(position - 1);

            return tr.fullRangeStartSec;
        }

        @Override
        public Object getItem(int position) {
            if (position == 0)
                return null;
            return gpsTrailerOverlay.sas.getTimeRange(position - 1);
        }

        @Override
        public int getCount() {
            int count = gpsTrailerOverlay.sas.getTimeRangeCount();
            //            Log.d(GTG.TAG,"item count is "+count);

            if (count >= 1)
                return count + 1;
            return 0;
        }

        @Override
        public boolean isEnabled(int position) {
            if (position == 0)
                return false;
            return true;
        }

        @Override
        public boolean areAllItemsEnabled() {
            return false;
        }
    });
}