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

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

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

TimeZone.getOffset介绍

暂无

代码示例

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

private Timestamp convertToTimestamp(Object object) {
  final long millis;
  if (object instanceof Long) {
    millis = (Long) object;
  } else {
    // use 'provided' Joda time
    final DateTime value = (DateTime) object;
    millis = value.toDate().getTime();
  }
  return new Timestamp(millis - LOCAL_TZ.getOffset(millis));
}

代码示例来源:origin: udacity/ud851-Sunshine

/**
 * Since all dates from the database are in UTC, we must convert the local date to the date in
 * UTC time. This function performs that conversion using the TimeZone offset.
 *
 * @param localDate The local datetime to convert to a UTC datetime, in milliseconds.
 * @return The UTC date (the local datetime + the TimeZone offset) in milliseconds.
 */
public static long getUTCDateFromLocal(long localDate) {
  TimeZone tz = TimeZone.getDefault();
  long gmtOffset = tz.getOffset(localDate);
  return localDate + gmtOffset;
}

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

TimeZone tz = TimeZone.getDefault();
Date now = new Date();
int offsetFromUtc = tz.getOffset(now.getTime()) / 1000;

代码示例来源:origin: opentripplanner/OpenTripPlanner

public void setTimeZone(TimeZone timeZone) {
    Calendar calendar = Calendar.getInstance(timeZone);
    calendar.setTime(startTime.getTime());
    startTime = calendar;
    calendar = Calendar.getInstance(timeZone);
    calendar.setTime(endTime.getTime());
    endTime = calendar;
    agencyTimeZoneOffset = timeZone.getOffset(startTime.getTimeInMillis());
  }
}

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

public static String getCurrentTimezoneOffset() {

  TimeZone tz = TimeZone.getDefault();  
  Calendar cal = GregorianCalendar.getInstance(tz);
  int offsetInMillis = tz.getOffset(cal.getTimeInMillis());

  String offset = String.format("%02d:%02d", Math.abs(offsetInMillis / 3600000), Math.abs((offsetInMillis / 60000) % 60));
  offset = (offsetInMillis >= 0 ? "+" : "-") + offset;

  return offset;
}

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

public static long toLocalDate(TimeZone browserTZ, TimeZone targetTZ, Date utcDate) {
    long utc = utcDate.getTime();
    int targetOffsetFromUTC = targetTZ.getOffset(utc);
    int browserOffsetFromUTC = browserTZ.getOffset(utc);

    return utc + targetOffsetFromUTC - browserOffsetFromUTC;
  }
}

代码示例来源:origin: jiangqqlmj/FastDev4Android

public static String getTimeArea() {
  return String.valueOf(
      TimeZone.getDefault().getOffset(new Date().getTime() / 1000))
      .toString();
}

代码示例来源:origin: udacity/ud851-Sunshine

/**
 * Since all dates from the database are in UTC, we must convert the local date to the date in
 * UTC time. This function performs that conversion using the TimeZone offset.
 *
 * @param localDate The local datetime to convert to a UTC datetime, in milliseconds.
 * @return The UTC date (the local datetime + the TimeZone offset) in milliseconds.
 */
public static long getUTCDateFromLocal(long localDate) {
  TimeZone tz = TimeZone.getDefault();
  long gmtOffset = tz.getOffset(localDate);
  return localDate + gmtOffset;
}

代码示例来源:origin: prestodb/presto

int offset = tz.getOffset(calendar.getTimeInMillis());
if (offset != 0) {
  int hours = Math.abs((offset / (60 * 1000)) / 60);

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

private String createArchiveFileName(final String originalFlowConfigFileName) {
  TimeZone tz = TimeZone.getDefault();
  Calendar cal = GregorianCalendar.getInstance(tz);
  int offsetInMillis = tz.getOffset(cal.getTimeInMillis());
  final int year = cal.get(Calendar.YEAR);
  final int month = cal.get(Calendar.MONTH) + 1;
  final int day = cal.get(Calendar.DAY_OF_MONTH);
  final int hour = cal.get(Calendar.HOUR_OF_DAY);
  final int min = cal.get(Calendar.MINUTE);
  final int sec = cal.get(Calendar.SECOND);
  String offset = String.format("%s%02d%02d",
      (offsetInMillis >= 0 ? "+" : "-"),
      Math.abs(offsetInMillis / 3600000),
      Math.abs((offsetInMillis / 60000) % 60));
  return String.format("%d%02d%02dT%02d%02d%02d%s_%s",
      year, month, day, hour, min, sec, offset, originalFlowConfigFileName);
}

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

public static Date toUtcDate(TimeZone browserTZ, TimeZone targetTZ, Date localDate) {
  if (localDate == null) {
    return null;
  }
  long local = localDate.getTime();
  int targetOffsetFromUTC = targetTZ.getOffset(local);
  int browserOffsetFromUTC = browserTZ.getOffset(local);
  return new Date(local - targetOffsetFromUTC + browserOffsetFromUTC);
}

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

// **** YOUR CODE **** BEGIN ****
 long ts = System.currentTimeMillis();
 Date localTime = new Date(ts);
 String format = "yyyy/MM/dd HH:mm:ss";
 SimpleDateFormat sdf = new SimpleDateFormat(format);
 // Convert Local Time to UTC (Works Fine)
 sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
 Date gmtTime = new Date(sdf.format(localTime));
 System.out.println("Local:" + localTime.toString() + "," + localTime.getTime() + " --> UTC time:"
     + gmtTime.toString() + "," + gmtTime.getTime());
 // **** YOUR CODE **** END ****
 // Convert UTC to Local Time
 Date fromGmt = new Date(gmtTime.getTime() + TimeZone.getDefault().getOffset(localTime.getTime()));
 System.out.println("UTC time:" + gmtTime.toString() + "," + gmtTime.getTime() + " --> Local:"
     + fromGmt.toString() + "-" + fromGmt.getTime());

代码示例来源:origin: udacity/ud851-Sunshine

/**
 * Since all dates from the database are in UTC, we must convert the local date to the date in
 * UTC time. This function performs that conversion using the TimeZone offset.
 *
 * @param localDate The local datetime to convert to a UTC datetime, in milliseconds.
 * @return The UTC date (the local datetime + the TimeZone offset) in milliseconds.
 */
public static long getUTCDateFromLocal(long localDate) {
  TimeZone tz = TimeZone.getDefault();
  long gmtOffset = tz.getOffset(localDate);
  return localDate + gmtOffset;
}

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

int offset = tz.getOffset(calendar.getTimeInMillis());
if (offset != 0) {
  int hours = Math.abs((offset / (60 * 1000)) / 60);

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

private Date convertToDate(Object object) {
  final long millis;
  if (object instanceof Integer) {
    final Integer value = (Integer) object;
    // adopted from Apache Calcite
    final long t = (long) value * 86400000L;
    millis = t - (long) LOCAL_TZ.getOffset(t);
  } else {
    // use 'provided' Joda time
    final LocalDate value = (LocalDate) object;
    millis = value.toDate().getTime();
  }
  return new Date(millis);
}

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

public VelocityContext getVelocityContext(Map<String, Object> modelMap, Class<? extends SparkController> controller, String viewName) {
  HashMap<String, Object> context = new HashMap<>(modelMap);
  context.put("railsAssetsService", railsAssetsService);
  context.put("webpackAssetsService", webpackAssetsService);
  context.put("securityService", securityService);
  context.put("maintenanceModeService", maintenanceModeService);
  context.put("currentUser", SessionUtils.currentUsername());
  context.put("controllerName", humanizedControllerName(controller));
  context.put("viewName", viewName);
  context.put("currentVersion", CurrentGoCDVersion.getInstance());
  context.put("toggles", Toggles.class);
  context.put("goUpdate", versionInfoService.getGoUpdate());
  context.put("goUpdateCheckEnabled", versionInfoService.isGOUpdateCheckEnabled());
  context.put("serverTimezoneUTCOffset", TimeZone.getDefault().getOffset(new Date().getTime()));
  context.put("spaRefreshInterval", SystemEnvironment.goSpaRefreshInterval());
  context.put("spaTimeout", SystemEnvironment.goSpaTimeout());
  context.put("showAnalyticsDashboard", showAnalyticsDashboard());
  context.put("devMode", !new SystemEnvironment().useCompressedJs());
  return new VelocityContext(context);
}

代码示例来源:origin: udacity/ud851-Sunshine

/**
 * Since all dates from the database are in UTC, we must convert the local date to the date in
 * UTC time. This function performs that conversion using the TimeZone offset.
 *
 * @param localDate The local datetime to convert to a UTC datetime, in milliseconds.
 * @return The UTC date (the local datetime + the TimeZone offset) in milliseconds.
 */
public static long getUTCDateFromLocal(long localDate) {
  TimeZone tz = TimeZone.getDefault();
  long gmtOffset = tz.getOffset(localDate);
  return localDate + gmtOffset;
}

代码示例来源:origin: com.thoughtworks.xstream/xstream

@Override
  public String toString(final Object obj) {
    final Calendar calendar = (Calendar)obj;
    final Instant instant = Instant.ofEpochMilli(calendar.getTimeInMillis());
    final int offsetInMillis = calendar.getTimeZone().getOffset(calendar.getTimeInMillis());
    final OffsetDateTime offsetDateTime = OffsetDateTime.ofInstant(instant, ZoneOffset.ofTotalSeconds(offsetInMillis
      / 1000));
    return STD_DATE_TIME.format(offsetDateTime);
  }
}

代码示例来源:origin: quartz-scheduler/quartz

/**
 * Translate a date & time from a users time zone to the another
 * (probably server) time zone to assist in creating a simple trigger with 
 * the right date & time.
 * 
 * @param date the date to translate
 * @param src the original time-zone
 * @param dest the destination time-zone
 * @return the translated date
 */
public static Date translateTime(Date date, TimeZone src, TimeZone dest) {
  Date newDate = new Date();
  int offset = (dest.getOffset(date.getTime()) - src.getOffset(date.getTime()));
  newDate.setTime(date.getTime() - offset);
  return newDate;
}

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

public FlowConfigurationDTO createFlowConfigurationDto(final String autoRefreshInterval,
                            final Long defaultBackPressureObjectThreshold,
                            final String defaultBackPressureDataSizeThreshold) {
  final FlowConfigurationDTO dto = new FlowConfigurationDTO();
  // get the refresh interval
  final long refreshInterval = FormatUtils.getTimeDuration(autoRefreshInterval, TimeUnit.SECONDS);
  dto.setAutoRefreshIntervalSeconds(refreshInterval);
  dto.setSupportsManagedAuthorizer(AuthorizerCapabilityDetection.isManagedAuthorizer(authorizer));
  dto.setSupportsConfigurableUsersAndGroups(AuthorizerCapabilityDetection.isConfigurableUserGroupProvider(authorizer));
  dto.setSupportsConfigurableAuthorizer(AuthorizerCapabilityDetection.isConfigurableAccessPolicyProvider(authorizer));
  final Date now = new Date();
  dto.setTimeOffset(TimeZone.getDefault().getOffset(now.getTime()));
  dto.setCurrentTime(now);
  dto.setDefaultBackPressureDataSizeThreshold(defaultBackPressureDataSizeThreshold);
  dto.setDefaultBackPressureObjectThreshold(defaultBackPressureObjectThreshold);
  return dto;
}

相关文章