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

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

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

TimeZone.getID介绍

[英]Gets the ID of this time zone.
[中]获取此时区的ID。

代码示例

代码示例来源:origin: spring-projects/spring-framework

/**
 * Parse the given {@code timeZoneString} value into a {@link TimeZone}.
 * @param timeZoneString the time zone {@code String}, following {@link TimeZone#getTimeZone(String)}
 * but throwing {@link IllegalArgumentException} in case of an invalid time zone specification
 * @return a corresponding {@link TimeZone} instance
 * @throws IllegalArgumentException in case of an invalid time zone specification
 */
public static TimeZone parseTimeZoneString(String timeZoneString) {
  TimeZone timeZone = TimeZone.getTimeZone(timeZoneString);
  if ("GMT".equals(timeZone.getID()) && !timeZoneString.startsWith("GMT")) {
    // We don't want that GMT fallback...
    throw new IllegalArgumentException("Invalid time zone specification '" + timeZoneString + "'");
  }
  return timeZone;
}

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

public static Date getDate(final SharedPreferences prefs, final String key, final Date defValue) {
  if (!prefs.contains(key + "_value") || !prefs.contains(key + "_zone")) {
    return defValue;
  }
  Calendar calendar = Calendar.getInstance();
  calendar.setTimeInMillis(prefs.getLong(key + "_value", 0));
  calendar.setTimeZone(TimeZone.getTimeZone(prefs.getString(key + "_zone", TimeZone.getDefault().getID())));
  return calendar.getTime();
}

public static void putDate(final SharedPreferences prefs, final String key, final Date date, final TimeZone zone) {
  editor.edit().putLong(key + "_value", date.getTime()).apply();
  editor.edit().putString(key + "_zone", zone.getID()).apply();
}

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

@Config("hive.time-zone")
public HiveClientConfig setTimeZone(String id)
{
  this.timeZone = (id != null) ? id : TimeZone.getDefault().getID();
  return this;
}

代码示例来源:origin: org.springframework/spring-core

/**
 * Parse the given {@code timeZoneString} value into a {@link TimeZone}.
 * @param timeZoneString the time zone {@code String}, following {@link TimeZone#getTimeZone(String)}
 * but throwing {@link IllegalArgumentException} in case of an invalid time zone specification
 * @return a corresponding {@link TimeZone} instance
 * @throws IllegalArgumentException in case of an invalid time zone specification
 */
public static TimeZone parseTimeZoneString(String timeZoneString) {
  TimeZone timeZone = TimeZone.getTimeZone(timeZoneString);
  if ("GMT".equals(timeZone.getID()) && !timeZoneString.startsWith("GMT")) {
    // We don't want that GMT fallback...
    throw new IllegalArgumentException("Invalid time zone specification '" + timeZoneString + "'");
  }
  return timeZone;
}

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

TimeZone timeZone = TimeZone.getTimeZone("UTC");
Calendar calendar = Calendar.getInstance(timeZone);
SimpleDateFormat simpleDateFormat = 
    new SimpleDateFormat("EE MMM dd HH:mm:ss zzz yyyy", Locale.US);
simpleDateFormat.setTimeZone(timeZone);

System.out.println("Time zone: " + timeZone.getID());
System.out.println("default time zone: " + TimeZone.getDefault().getID());
System.out.println();

System.out.println("UTC:     " + simpleDateFormat.format(calendar.getTime()));
System.out.println("Default: " + calendar.getTime());

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

TimeZone tz = TimeZone.getDefault();
System.out.println("TimeZone   "+tz.getDisplayName(false, TimeZone.SHORT)+" Timezon id :: " +tz.getID());

代码示例来源:origin: alibaba/easyexcel

/**
 * Parse the given {@code timeZoneString} value into a {@link TimeZone}.
 * @param timeZoneString the time zone {@code String}, following {@link TimeZone#getTimeZone(String)}
 * but throwing {@link IllegalArgumentException} in case of an invalid time zone specification
 * @return a corresponding {@link TimeZone} instance
 * @throws IllegalArgumentException in case of an invalid time zone specification
 */
public static TimeZone parseTimeZoneString(String timeZoneString) {
  TimeZone timeZone = TimeZone.getTimeZone(timeZoneString);
  if ("GMT".equals(timeZone.getID()) && !timeZoneString.startsWith("GMT")) {
    // We don't want that GMT fallback...
    throw new IllegalArgumentException("Invalid time zone specification '" + timeZoneString + "'");
  }
  return timeZone;
}

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

public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
  reader.moveDown();
  long timeInMillis = Long.parseLong(reader.getValue());
  reader.moveUp();
  final String timeZone;
  if (reader.hasMoreChildren()) {
    reader.moveDown();
    timeZone = reader.getValue();
    reader.moveUp();
  } else { // backward compatibility to XStream 1.1.2 and below
    timeZone = TimeZone.getDefault().getID();
  }
  GregorianCalendar result = new GregorianCalendar();
  result.setTimeZone(TimeZone.getTimeZone(timeZone));
  result.setTime(new Date(timeInMillis)); // calendar.setTimeInMillis() not available under JDK 1.3
  return result;
}

代码示例来源: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: org.freemarker/freemarker

public TimeZone build() {
  TimeZone timeZone = TimeZone.getTimeZone(timeZoneId);
  if (timeZone.getID().equals("GMT") && !timeZoneId.equals("GMT") && !timeZoneId.equals("UTC")
      && !timeZoneId.equals("GMT+00") && !timeZoneId.equals("GMT+00:00") && !timeZoneId.equals("GMT+0000")) {
    throw new IllegalArgumentException("Unrecognized time zone: " + timeZoneId);
  }
  return timeZone;
}

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

@OnScheduled
public void OnScheduled(final ProcessContext context) {
  // Configure jackson mapper before spawning onTriggers
  final SimpleModule module = new SimpleModule()
                  .addSerializer(MacAddress.class, new MacAddressToStringSerializer());
  mapper.registerModule(module);
  mapper.setDateFormat(this.simpleDateFormat);
  switch (context.getProperty(TIME_REPRESENTATION).getValue()) {
    case LOCAL_TZ:
      // set the mapper TZ to local TZ
      mapper.setTimeZone(TimeZone.getDefault());
      tzId = TimeZone.getDefault().getID();
      break;
    case UTC:
      // set the mapper TZ to local TZ
      mapper.setTimeZone(TimeZone.getTimeZone(UTC));
      tzId = UTC;
      break;
  }
}

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

String timezoneID = TimeZone.getDefault().getID();
  System.out.println(timezoneID);

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

@Implementation(maxSdk = KITKAT_WATCH)
protected boolean parse(String timeString) {
 TimeZone tz;
 if (timeString.endsWith("Z")) {
  timeString = timeString.substring(0, timeString.length() - 1);
  tz = TimeZone.getTimeZone("UTC");
 } else {
  tz = TimeZone.getTimeZone(time.timezone);
 }
 SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd'T'HHmmss", Locale.ENGLISH);
 SimpleDateFormat dfShort = new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH);
 df.setTimeZone(tz);
 dfShort.setTimeZone(tz);
 time.timezone = tz.getID();
 try {
  set(df.parse(timeString).getTime());
 } catch (ParseException e) {
  try {
   set(dfShort.parse(timeString).getTime());
  } catch (ParseException e2) {
   throwTimeFormatException(e2.getLocalizedMessage());
  }
 }
 return "UTC".equals(tz.getID());
}

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

@Before
public void setUp() {
 context = ApplicationProvider.getApplicationContext();
 alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
 shadowAlarmManager = shadowOf(alarmManager);
 activity = Robolectric.setupActivity(Activity.class);
 TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
 assertThat(TimeZone.getDefault().getID()).isEqualTo("America/Los_Angeles");
}

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

@Implementation(maxSdk = KITKAT_WATCH)
protected static String getCurrentTimezone() {
 return TimeZone.getDefault().getID();
}

代码示例来源:origin: alibaba/fastjson

if (index > 20) {
  String tzStr = strVal.substring(index + 1);
  TimeZone timeZone = TimeZone.getTimeZone(tzStr);
  if (!"GMT".equals(timeZone.getID())) {
    String subStr = strVal.substring(0, index);
    JSONScanner dateLexer = new JSONScanner(subStr);

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

return createDateFormat(pattern, TimeZone.getDefault());
return createDateFormat(ISO_FORMAT_WITH_TZ, TimeZone.getDefault());
final TimeZone defaultTZ = TimeZone.getDefault();
final int defaultOffset = defaultTZ.getOffset(nowInMillis) / MILLIS_IN_HOUR;
final int userOffset = TimeZone.getTimeZone(System
    .getProperty("user.timezone")).getOffset(nowInMillis) / MILLIS_IN_HOUR;
final Locale defaultLocale = Locale.getDefault();
System.out.println("  default.timezone.id = " + defaultTZ.getID());
System.out.println("  default.timezone-offset (hours) = " + defaultOffset);
System.out.println("  default.locale = "
londonTimeZone = TimeZone.getTimeZone("GMT");
newYorkTimeZone = TimeZone.getTimeZone("GMT-5");
sydneyTimeZone = TimeZone.getTimeZone("GMT+10");

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

String string = "2013-03-05T18:05:05.000Z";
String defaultTimezone = TimeZone.getDefault().getID();
Date date = (new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")).parse(string.replaceAll("Z$", "+0000"));

System.out.println("string: " + string);
System.out.println("defaultTimezone: " + defaultTimezone);
System.out.println("date: " + (new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")).format(date));

代码示例来源:origin: org.freemarker/freemarker

/**
 * Returns the time zone object for the name (or ID). This differs from
 * {@link TimeZone#getTimeZone(String)} in that the latest returns GMT
 * if it doesn't recognize the name, while this throws an
 * {@link UnrecognizedTimeZoneException}.
 * 
 * @throws UnrecognizedTimeZoneException If the time zone name wasn't understood
 */
public static TimeZone getTimeZone(String name)
throws UnrecognizedTimeZoneException {
  if (isGMTish(name)) {
    if (name.equalsIgnoreCase("UTC")) {
      return UTC;
    }
    return TimeZone.getTimeZone(name);
  }
  TimeZone tz = TimeZone.getTimeZone(name);
  if (isGMTish(tz.getID())) {
    throw new UnrecognizedTimeZoneException(name);
  }
  return tz;
}

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

@Test
 public void testConvertFromTimestamp2() {
  TimeZone defaultZone = TimeZone.getDefault();
  try {
   // Use system zone when converting from timestamp to timestamptz
   String s = "2017-06-12 23:12:56.34";
   TimestampTZ tstz1 = TimestampTZUtil.parse(s + " " + TimeZone.getTimeZone("Europe/London").getID());
   TimestampTZ tstz2 = TimestampTZUtil.parse(s + " " + TimeZone.getTimeZone("America/Los_Angeles").getID());
   Assert.assertTrue(tstz1.compareTo(tstz2) < 0);
  } finally {
   TimeZone.setDefault(defaultZone);
  }
 }
}

相关文章