下面列出了java.time.ZoneId#systemDefault ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
ZoneId zoneId = ZoneId.systemDefault();
LocalDateTime start = LocalDateTime.ofInstant(
Instant.ofEpochMilli(Long.parseLong(this.start)), zoneId);
LocalDateTime end = LocalDateTime.ofInstant(Instant.ofEpochMilli(Long.parseLong(this.end)),
zoneId);
sb.append(duration).append("ms|");
sb.append(query).append('|');
sb.append(start).append('|');
sb.append(end).append('|');
sb.append(step).append("s|");
sb.append(exclusions != null ? exclusions.toString() : "");
return sb.toString();
}
@Override
public void run(RestContext context) {
final EntityManager em = ((EntityManageable) context.getRequest()).getEntityManager();
LicenseKey licenseKey = some(context.getRequest().getCookies(),
c->c.get(config.getCookieName()),
Cookie::getValue,
LicenseKey::new).orElse(null);
if (licenseKey == null) return;
final CriteriaBuilder cb = em.getCriteriaBuilder();
final CriteriaQuery<UserLicense> query = cb.createQuery(UserLicense.class);
final Root<UserLicense> root = query.from(UserLicense.class);
query.where(cb.equal(root.get("licenseKey"), licenseKey.asBytes()));
em.createQuery(query).getResultStream()
.forEach(em::remove);
Cookie cookie = Cookie.create(config.getCookieName(), licenseKey.asString());
ZoneId zone = ZoneId.systemDefault();
Date expires = Date.from(
ZonedDateTime.of(LocalDate.now()
.minusYears(10)
.atTime(0, 0), zone)
.toInstant());
cookie.setExpires(expires);
cookie.setPath("/");
context.setHeaders(Headers.of("Set-Cookie", cookie.toHttpString()));
}
@Test(expectedExceptions = ZoneRulesException.class)
public void test_systemDefault_unableToConvert_unknownId() {
TimeZone current = TimeZone.getDefault();
try {
TimeZone.setDefault(new SimpleTimeZone(127, "SomethingWeird"));
ZoneId.systemDefault();
} finally {
TimeZone.setDefault(current);
}
}
@Test(expectedExceptions = ZoneRulesException.class)
public void test_systemDefault_unableToConvert_unknownId() {
TimeZone current = TimeZone.getDefault();
try {
TimeZone.setDefault(new SimpleTimeZone(127, "SomethingWeird"));
ZoneId.systemDefault();
} finally {
TimeZone.setDefault(current);
}
}
@Test(expectedExceptions = ZoneRulesException.class)
public void test_systemDefault_unableToConvert_unknownId() {
TimeZone current = TimeZone.getDefault();
try {
TimeZone.setDefault(new SimpleTimeZone(127, "SomethingWeird"));
ZoneId.systemDefault();
} finally {
TimeZone.setDefault(current);
}
}
/**
* @param id the ID/name of MySQL time zone
* @return the {@link ZoneId} from {@code id}, or system default timezone if not found.
*/
private static ZoneId convertZoneId(String id) {
String realId;
if (id.startsWith(ZONE_PREFIX_POSIX) || id.startsWith(ZONE_PREFIX_RIGHT)) {
realId = id.substring(PREFIX_LENGTH);
} else {
realId = id;
}
try {
switch (realId) {
case "Factory":
// Looks like the "Factory" time zone is UTC.
return ZoneOffset.UTC;
case "America/Nuuk":
// They are same timezone including DST.
return ZoneId.of("America/Godthab");
case "ROC":
// Republic of China, 1912-1949, very very old time zone.
// Even the ZoneId.SHORT_IDS does not support it.
// Is there anyone using this time zone, really?
// Don't think so, but should support it for compatible.
// Just use GMT+8, id is equal to +08:00.
return ZoneId.of("+8");
default:
return ZoneId.of(realId, ZoneId.SHORT_IDS);
}
} catch (DateTimeException e) {
logger.warn("The server timezone is <{}> that's unknown, trying to use system default timezone", id);
return ZoneId.systemDefault();
}
}
@Test(expectedExceptions = DateTimeException.class)
public void test_systemDefault_unableToConvert_badFormat() {
TimeZone current = TimeZone.getDefault();
try {
TimeZone.setDefault(new SimpleTimeZone(127, "Something Weird"));
ZoneId.systemDefault();
} finally {
TimeZone.setDefault(current);
}
}
@Test(expectedExceptions = ZoneRulesException.class)
public void test_systemDefault_unableToConvert_unknownId() {
TimeZone current = TimeZone.getDefault();
try {
TimeZone.setDefault(new SimpleTimeZone(127, "SomethingWeird"));
ZoneId.systemDefault();
} finally {
TimeZone.setDefault(current);
}
}
/**
* Simulate a job that has run longer than the next fire time such that it misfires, but will not fire again because
* the end of the trigger window has passed.
*/
@Test
public void testTriggerPastDueMisfireButWillNotFire() throws Exception {
JobDetail jobDetail = JobBuilder.newJob(MyNonConcurrentJob.class).storeDurably(true).build();
jobStore.storeJob(jobDetail, false);
ZoneId zone = ZoneId.systemDefault();
Date baseFireTimeDate = DateBuilder.evenMinuteDateAfterNow();
LocalDateTime baseDateTime = LocalDateTime.ofInstant(baseFireTimeDate.toInstant(), zone);
LocalDateTime startAt = baseDateTime.minusMinutes(5);
LocalDateTime endAt = baseDateTime.minusMinutes(1);
LocalDateTime nextFireTime = startAt.plusMinutes(1);
SimpleScheduleBuilder simple = SimpleScheduleBuilder.simpleSchedule()
.withIntervalInMinutes(1);
OperableTrigger trigger = (OperableTrigger) TriggerBuilder.newTrigger()
.forJob(jobDetail)
.withSchedule(simple)
.startAt(Date.from(startAt.atZone(zone).toInstant()))
.endAt(Date.from(endAt.atZone(zone).toInstant()))
.build();
// misfire the trigger and set the next fire time in the past
trigger.updateAfterMisfire(null);
trigger.setNextFireTime(Date.from(nextFireTime.atZone(zone).toInstant()));
trigger.setMisfireInstruction(MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_EXISTING_COUNT);
jobStore.storeTrigger(trigger, false);
List<OperableTrigger> acquiredTriggers =
jobStore.acquireNextTriggers(baseFireTimeDate.getTime(), 4, 1000L);
assertEquals(0, acquiredTriggers.size());
}
@Test(expectedExceptions = ZoneRulesException.class)
public void test_systemDefault_unableToConvert_unknownId() {
TimeZone current = TimeZone.getDefault();
try {
TimeZone.setDefault(new SimpleTimeZone(127, "SomethingWeird"));
ZoneId.systemDefault();
} finally {
TimeZone.setDefault(current);
}
}
public static ZoneId getZoneId() {
if (ZONE_ID == null) {
ZONE_ID = ZoneId.systemDefault();
}
return ZONE_ID;
}
/**
* 将long类型的timestamp转为LocalDateTime
*/
public static LocalDateTime getDateTimeOfTimestamp(long timestamp) {
Instant instant = Instant.ofEpochMilli(timestamp);
ZoneId zone = ZoneId.systemDefault();
return LocalDateTime.ofInstant(instant, zone);
}
public void test_systemDefault() {
ZoneId test = ZoneId.systemDefault();
assertEquals(test.getId(), TimeZone.getDefault().getID());
}
private static ClientSession buildClientSession(String server, String user, String source, String catalog, String schema, Map<String, String> properties) {
return new ClientSession(URI.create(server), user, source, Optional.empty(), ImmutableSet.of(), null, catalog,
schema, null, ZoneId.systemDefault(), Locale.getDefault(),
ImmutableMap.of(), properties, emptyMap(), emptyMap(), ImmutableMap.of(), null, new Duration(2, MINUTES));
}
@Test
void shouldNotSetServerZoneId() {
ConnectionContext context = new ConnectionContext(ZeroDateOption.USE_NULL, ZoneId.systemDefault());
assertThat(context.shouldSetServerZoneId()).isFalse();
}
@Test
void badSetServerZoneId() {
ConnectionContext context = new ConnectionContext(ZeroDateOption.USE_NULL, ZoneId.systemDefault());
assertThatIllegalStateException().isThrownBy(() -> context.setServerZoneId(ZoneId.systemDefault()));
}
@Test
void shouldNotSetServerZoneId() {
ConnectionContext context = new ConnectionContext(ZeroDateOption.USE_NULL, ZoneId.systemDefault());
assertThat(context.shouldSetServerZoneId()).isFalse();
}
public static Object resolveZoneId(HttpServletRequest request) {
TimeZone timeZone = RequestContextUtils.getTimeZone(request);
return (timeZone != null ? timeZone.toZoneId() : ZoneId.systemDefault());
}
/**
* Creates a new {@code DateTimeBucketer} with the given date/time format string using JVM's default timezone.
*
* @param formatString The format string that will be given to {@code DateTimeFormatter} to determine
* the bucket path.
*/
public DateTimeBucketer(String formatString) {
this(formatString, ZoneId.systemDefault());
}
/**
* Creates a new {@code DateTimeBucketer} with the given date/time format string using JVM's default timezone.
*
* @param formatString The format string that will be given to {@code DateTimeFormatter} to determine
* the bucket path.
*/
public DateTimeBucketer(String formatString) {
this(formatString, ZoneId.systemDefault());
}