TimeZone.getDefault().inDaylightTime( new Date() );
private static void refreshTimezone() { try { TimeZone tz = TimeZone.getDefault(); deviceTimezoneAbbreviation = tz.getDisplayName( tz.inDaylightTime(new Date()), TimeZone.SHORT ); deviceTimeZoneName = tz.getID(); } catch (AssertionError e) { // Workaround for a bug in Android that can cause crashes on Android 8.0 and 8.1 } catch (Exception e) { } }
TimeZone.getTimeZone( "US/Alaska").inDaylightTime( new Date() );
Calendar c = Calendar.getInstance(); System.out.println("current: "+c.getTime()); TimeZone z = c.getTimeZone(); int offset = z.getRawOffset(); if(z.inDaylightTime(new Date())){ offset = offset + z.getDSTSavings(); } int offsetHrs = offset / 1000 / 60 / 60; int offsetMins = offset / 1000 / 60 % 60; System.out.println("offset: " + offsetHrs); System.out.println("offset: " + offsetMins); c.add(Calendar.HOUR_OF_DAY, (-offsetHrs)); c.add(Calendar.MINUTE, (-offsetMins)); System.out.println("GMT Time: "+c.getTime());
/** * Returns the offset in milliseconds from UTC for this time zone at {@code * time}. The offset includes daylight savings time if the specified * date is within the daylight savings time period. * * @param time the date in milliseconds since January 1, 1970 00:00:00 UTC */ public int getOffset(long time) { if (inDaylightTime(new Date(time))) { return getRawOffset() + getDSTSavings(); } return getRawOffset(); }
if (tz.inDaylightTime(new Date())) { rawOffset += tz.getDSTSavings();
@Override protected void computeFields() { TimeZone timeZone = getTimeZone(); int dstOffset = timeZone.inDaylightTime(new Date(time)) ? timeZone.getDSTSavings() : 0; int zoneOffset = timeZone.getRawOffset(); fields[DST_OFFSET] = dstOffset; fields[ZONE_OFFSET] = zoneOffset; fullFieldsCalc(time, zoneOffset); for (int i = 0; i < FIELD_COUNT; i++) { isSet[i] = true; } }
/** * Returns time zone display name or ID for the specified date-time value. * * @param value * value * @param tzd * if {@code true} return TZD (time zone region with Daylight Saving * Time information included), if {@code false} return TZR (time zone * region) * @return time zone display name or ID */ private static String getTimeZone(Value value, boolean tzd) { if (!(value instanceof ValueTimestampTimeZone)) { TimeZone tz = TimeZone.getDefault(); if (tzd) { boolean daylight = tz.inDaylightTime(value.getTimestamp()); return tz.getDisplayName(daylight, TimeZone.SHORT); } return tz.getID(); } return DateTimeUtils.timeZoneNameFromOffsetMins(((ValueTimestampTimeZone) value).getTimeZoneOffsetMins()); }
if (zone.inDaylightTime(new Date()) && zone.useDaylightTime()) { offset += (int)JiveConstants.HOUR;
/** formats time zone specifier. */ private static void formatTimeZone(Calendar cal,StringBuilder buf) { TimeZone tz = cal.getTimeZone(); if (tz == null) return; // otherwise print out normally. int offset; if (tz.inDaylightTime(cal.getTime())) { offset = tz.getRawOffset() + (tz.useDaylightTime()?3600000:0); } else { offset = tz.getRawOffset(); } if(offset==0) { buf.append('Z'); return; } if (offset >= 0) buf.append('+'); else { buf.append('-'); offset *= -1; } offset /= 60 * 1000; // offset is in milli-seconds formatTwoDigits(offset / 60, buf); buf.append(':'); formatTwoDigits(offset % 60, buf); }
appendTwoDigits(result, cal.get(Calendar.SECOND)); result.append(' '); result.append(tz.getDisplayName(tz.inDaylightTime(this), TimeZone.SHORT)); result.append(' '); result.append(cal.get(Calendar.YEAR));
/** * Get the DST offset (if any) for the default locale and the given date. * * @param self a Date * @return the DST offset as a Duration. */ public static Duration getDaylightSavingsOffset(Date self) { TimeZone timeZone = getTimeZone(self); int millis = (timeZone.useDaylightTime() && timeZone.inDaylightTime(self)) ? timeZone.getDSTSavings() : 0; return new TimeDuration(0, 0, 0, millis); }
public static Memory gettimeofday(boolean getAsFloat){ long msec_time = System.currentTimeMillis(); if (getAsFloat) { double now = msec_time / 1000.0; return new DoubleMemory(now); } else { ArrayMemory result = new ArrayMemory(); TimeZone timeZone = TimeZone.getDefault(); long sec = msec_time / 1000; long usec = (msec_time % 1000) * 1000; long minuteswest = - timeZone.getOffset(msec_time) / MSEC_IN_MIN; boolean is_dst = timeZone.inDaylightTime(new Date(msec_time)); result.refOfIndex("sec").assign(sec); result.refOfIndex("usec").assign(usec); result.refOfIndex("minuteswest").assign(minuteswest); result.refOfIndex("dsttime").assign(is_dst ? 1 : 0); return result.toConstant(); } }
case 'Z': TimeZone timeZone = calendar.getTimeZone(); result.append(timeZone.getDisplayName(timeZone.inDaylightTime(calendar.getTime()), TimeZone.SHORT, locale)); return true;
@Test public void testGetTimeZone() throws Exception { try (LocalTestServerContext sc = new LocalTestServerContext( "\n" + "{\n" + " \"dstOffset\" : 0,\n" + " \"rawOffset\" : 36000,\n" + " \"status\" : \"OK\",\n" + " \"timeZoneId\" : \"Australia/Sydney\",\n" + " \"timeZoneName\" : \"Australian Eastern Standard Time\"\n" + "}\n")) { LatLng sydney = new LatLng(-33.8688, 151.2093); TimeZone tz = TimeZoneApi.getTimeZone(sc.context, sydney).await(); assertNotNull(tz); assertEquals(TimeZone.getTimeZone("Australia/Sydney"), tz); // GMT+10 assertEquals(36000000, tz.getRawOffset()); // DST is +1h assertEquals(3600000, tz.getDSTSavings()); assertTrue(tz.inDaylightTime(new Date(1388494800000L))); sc.assertParamValue(sydney.toUrlValue(), "location"); } }
/** * Create a TimeZoneDefinition compatible with java.util.TimeZone * @param timeZone a java time zone object, will be converted to Microsoft timezone. */ public OlsonTimeZoneDefinition(TimeZone timeZone) { final String microsoftTimeZoneName = TimeZoneUtils.getMicrosoftTimeZoneName(timeZone); if (microsoftTimeZoneName != null) { this.id = microsoftTimeZoneName; } this.name = timeZone.getDisplayName(timeZone.inDaylightTime(new Date()), TimeZone.LONG); }
Calendar calendar = Calendar.getInstance(); TimeZone fromTimeZone = calendar.getTimeZone(); TimeZone toTimeZone = TimeZone.getTimeZone("CST"); calendar.setTimeZone(fromTimeZone); calendar.add(Calendar.MILLISECOND, fromTimeZone.getRawOffset() * -1); if (fromTimeZone.inDaylightTime(calendar.getTime())) { calendar.add(Calendar.MILLISECOND, calendar.getTimeZone().getDSTSavings() * -1); } calendar.add(Calendar.MILLISECOND, toTimeZone.getRawOffset()); if (toTimeZone.inDaylightTime(calendar.getTime())) { calendar.add(Calendar.MILLISECOND, toTimeZone.getDSTSavings()); } System.out.println(calendar.getTime());
private Date computeAdjustedTime(Date initialDate, TimeZone targetTZ) { Calendar cal = Calendar.getInstance(); cal.setTime(initialDate); TimeZone localTZ = cal.getTimeZone(); int timezoneOffset = cal.get(Calendar.ZONE_OFFSET); if (localTZ.inDaylightTime(initialDate)) { timezoneOffset += (60 * 60 * 1000); } cal.add(Calendar.MILLISECOND, timezoneOffset); Date newTime = cal.getTime(); Calendar calTS = Calendar.getInstance(targetTZ); calTS.setTime(newTime); timezoneOffset = calTS.get(Calendar.ZONE_OFFSET); if (targetTZ.inDaylightTime(initialDate)) { timezoneOffset += (60 * 60 * 1000); } calTS.add(Calendar.MILLISECOND, -1 * timezoneOffset); return calTS.getTime(); }
@Override public Date computeAdjustedTime(final Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); TimeZone localTZ = cal.getTimeZone(); int timezoneOffset = cal.get(Calendar.ZONE_OFFSET); if (localTZ.inDaylightTime(date)) { timezoneOffset += (60 * 60 * 1000); } cal.add(Calendar.MILLISECOND, timezoneOffset); Date newTime = cal.getTime(); Calendar calTS = Calendar.getInstance(_usageTimezone); calTS.setTime(newTime); timezoneOffset = calTS.get(Calendar.ZONE_OFFSET); if (_usageTimezone.inDaylightTime(date)) { timezoneOffset += (60 * 60 * 1000); } calTS.add(Calendar.MILLISECOND, -1 * timezoneOffset); return calTS.getTime(); }
public static void main(String[] args) throws IOException { Test test=new Test(); Date fromDate = Calendar.getInstance().getTime(); System.out.println("UTC Time - "+fromDate); System.out.println("GMT Time - "+test.cvtToGmt(fromDate)); } private Date cvtToGmt( Date date ){ TimeZone tz = TimeZone.getDefault(); Date ret = new Date( date.getTime() - tz.getRawOffset() ); // if we are now in DST, back off by the delta. Note that we are checking the GMT date, this is the KEY. if ( tz.inDaylightTime( ret )){ Date dstDate = new Date( ret.getTime() - tz.getDSTSavings() ); // check to make sure we have not crossed back into standard time // this happens when we are on the cusp of DST (7pm the day before the change for PDT) if ( tz.inDaylightTime( dstDate )){ ret = dstDate; } } return ret; }