java.util.TimeZone.inDaylightTime()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(9.0k)|赞(0)|评价(0)|浏览(165)

本文整理了Java中java.util.TimeZone.inDaylightTime()方法的一些代码示例,展示了TimeZone.inDaylightTime()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。TimeZone.inDaylightTime()方法的具体详情如下:
包路径:java.util.TimeZone
类名称:TimeZone
方法名:inDaylightTime

TimeZone.inDaylightTime介绍

[英]Queries if the given date is in Daylight Saving Time in this time zone.
[中]查询给定日期是否在该时区的夏令时。

代码示例

代码示例来源:origin: stackoverflow.com

TimeZone.getDefault().inDaylightTime( new Date() );

代码示例来源:origin: facebook/facebook-android-sdk

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) {
  }
}

代码示例来源:origin: stackoverflow.com

TimeZone.getTimeZone( "US/Alaska").inDaylightTime( new Date() );

代码示例来源:origin: stackoverflow.com

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());

代码示例来源:origin: robovm/robovm

/**
 * 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();
}

代码示例来源:origin: stackoverflow.com

if (tz.inDaylightTime(new Date())) {
  rawOffset += tz.getDSTSavings();

代码示例来源:origin: robovm/robovm

@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;
  }
}

代码示例来源:origin: com.h2database/h2

/**
 * 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());
}

代码示例来源:origin: igniterealtime/Openfire

if (zone.inDaylightTime(new Date()) && zone.useDaylightTime()) {
  offset += (int)JiveConstants.HOUR;

代码示例来源:origin: JZ-Darkal/AndroidHttpCapture

/** 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);
}

代码示例来源:origin: robovm/robovm

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));

代码示例来源:origin: org.codehaus.groovy/groovy

/**
 * 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);
}

代码示例来源:origin: jphp-group/jphp

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();
}
}

代码示例来源:origin: robovm/robovm

case 'Z':
  TimeZone timeZone = calendar.getTimeZone();
  result.append(timeZone.getDisplayName(timeZone.inDaylightTime(calendar.getTime()),
      TimeZone.SHORT, locale));
  return true;

代码示例来源:origin: googlemaps/google-maps-services-java

@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");
 }
}

代码示例来源:origin: OfficeDev/ews-java-api

/**
 * 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);
}

代码示例来源:origin: stackoverflow.com

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());

代码示例来源:origin: apache/cloudstack

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();
}

代码示例来源:origin: apache/cloudstack

@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();
}

代码示例来源:origin: stackoverflow.com

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;
}

相关文章