下面列出了java.time.ZoneId#getId ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public boolean format(DateTimePrintContext context, StringBuilder buf) {
ZoneId zone = context.getValue(TemporalQueries.zoneId());
if (zone == null) {
return false;
}
String zname = zone.getId();
if (!(zone instanceof ZoneOffset)) {
TemporalAccessor dt = context.getTemporal();
String name = getDisplayName(zname,
dt.isSupported(ChronoField.INSTANT_SECONDS)
? (zone.getRules().isDaylightSavings(Instant.from(dt)) ? DST : STD)
: GENERIC,
context.getLocale());
if (name != null) {
zname = name;
}
}
buf.append(zname);
return true;
}
@Override
public boolean format(DateTimePrintContext context, StringBuilder buf) {
ZoneId zone = context.getValue(TemporalQueries.zoneId());
if (zone == null) {
return false;
}
String zname = zone.getId();
if (!(zone instanceof ZoneOffset)) {
TemporalAccessor dt = context.getTemporal();
String name = getDisplayName(zname,
dt.isSupported(ChronoField.INSTANT_SECONDS)
? (zone.getRules().isDaylightSavings(Instant.from(dt)) ? DST : STD)
: GENERIC,
context.getLocale());
if (name != null) {
zname = name;
}
}
buf.append(zname);
return true;
}
@Override
public boolean format(DateTimePrintContext context, StringBuilder buf) {
ZoneId zone = context.getValue(TemporalQueries.zoneId());
if (zone == null) {
return false;
}
String zname = zone.getId();
if (!(zone instanceof ZoneOffset)) {
TemporalAccessor dt = context.getTemporal();
String name = getDisplayName(zname,
dt.isSupported(ChronoField.INSTANT_SECONDS)
? (zone.getRules().isDaylightSavings(Instant.from(dt)) ? DST : STD)
: GENERIC,
context.getLocale());
if (name != null) {
zname = name;
}
}
buf.append(zname);
return true;
}
/**
* Gets the {@code TimeZone} for the given {@code zoneId}.
*
* @param zoneId a {@link ZoneId} from which the time zone ID is obtained
* @return the specified {@code TimeZone}, or the GMT zone if the given ID
* cannot be understood.
* @throws NullPointerException if {@code zoneId} is {@code null}
* @since 1.8
*/
public static TimeZone getTimeZone(ZoneId zoneId) {
String tzid = zoneId.getId(); // throws an NPE if null
char c = tzid.charAt(0);
if (c == '+' || c == '-') {
tzid = "GMT" + tzid;
} else if (c == 'Z' && tzid.length() == 1) {
tzid = "UTC";
}
return getTimeZone(tzid, true);
}
/**
* Gets the {@code TimeZone} for the given {@code zoneId}.
*
* @param zoneId a {@link ZoneId} from which the time zone ID is obtained
* @return the specified {@code TimeZone}, or the GMT zone if the given ID
* cannot be understood.
* @throws NullPointerException if {@code zoneId} is {@code null}
* @since 1.8
*/
public static TimeZone getTimeZone(ZoneId zoneId) {
String tzid = zoneId.getId(); // throws an NPE if null
char c = tzid.charAt(0);
if (c == '+' || c == '-') {
tzid = "GMT" + tzid;
} else if (c == 'Z' && tzid.length() == 1) {
tzid = "UTC";
}
return getTimeZone(tzid, true);
}
/**
* Gets the {@code TimeZone} for the given {@code zoneId}.
*
* @param zoneId a {@link ZoneId} from which the time zone ID is obtained
* @return the specified {@code TimeZone}, or the GMT zone if the given ID
* cannot be understood.
* @throws NullPointerException if {@code zoneId} is {@code null}
* @since 1.8
*/
public static TimeZone getTimeZone(ZoneId zoneId) {
String tzid = zoneId.getId(); // throws an NPE if null
char c = tzid.charAt(0);
if (c == '+' || c == '-') {
tzid = "GMT" + tzid;
} else if (c == 'Z' && tzid.length() == 1) {
tzid = "UTC";
}
return getTimeZone(tzid, true);
}
/**
* Gets the {@code TimeZone} for the given {@code zoneId}.
*
* @param zoneId a {@link ZoneId} from which the time zone ID is obtained
* @return the specified {@code TimeZone}, or the GMT zone if the given ID
* cannot be understood.
* @throws NullPointerException if {@code zoneId} is {@code null}
* @since 1.8
*/
public static TimeZone getTimeZone(ZoneId zoneId) {
String tzid = zoneId.getId(); // throws an NPE if null
char c = tzid.charAt(0);
if (c == '+' || c == '-') {
tzid = "GMT" + tzid;
} else if (c == 'Z' && tzid.length() == 1) {
tzid = "UTC";
}
return getTimeZone(tzid, true);
}
/**
* Gets the {@code TimeZone} for the given {@code zoneId}.
*
* @param zoneId a {@link ZoneId} from which the time zone ID is obtained
* @return the specified {@code TimeZone}, or the GMT zone if the given ID
* cannot be understood.
* @throws NullPointerException if {@code zoneId} is {@code null}
* @since 1.8
*/
public static TimeZone getTimeZone(ZoneId zoneId) {
String tzid = zoneId.getId(); // throws an NPE if null
char c = tzid.charAt(0);
if (c == '+' || c == '-') {
tzid = "GMT" + tzid;
} else if (c == 'Z' && tzid.length() == 1) {
tzid = "UTC";
}
return getTimeZone(tzid, true);
}
@Test
public void testIsTriggerWhenTheCurrentTimeEqualsWithTheNextTriggerAndZonesAreEquivalentThenItShouldTriggerAnEvent() {
ZoneId zone = ZoneId.systemDefault();
String timeZone = zone.getId();
ZonedDateTime currentZonedTime = ZonedDateTime.of(2017, 12, 20, 12, 0, 0, 0, zone.normalized());
long monitorUpdateRate = 60000L;
when(dateTimeService.getDefaultZonedDateTime()).thenReturn(currentZonedTime);
when(dateTimeService.getZonedDateTime(currentZonedTime.toInstant(), timeZone)).thenReturn(currentZonedTime);
TimeAlert timeAlert = createTimeAlert(timeZone);
assertTrue(underTest.isTrigger(timeAlert, monitorUpdateRate));
}
/**
* Gets the {@code TimeZone} for the given {@code zoneId}.
*
* @param zoneId a {@link ZoneId} from which the time zone ID is obtained
* @return the specified {@code TimeZone}, or the GMT zone if the given ID
* cannot be understood.
* @throws NullPointerException if {@code zoneId} is {@code null}
* @since 1.8
*/
public static TimeZone getTimeZone(ZoneId zoneId) {
String tzid = zoneId.getId(); // throws an NPE if null
char c = tzid.charAt(0);
if (c == '+' || c == '-') {
tzid = "GMT" + tzid;
} else if (c == 'Z' && tzid.length() == 1) {
tzid = "UTC";
}
return TimeZone.getTimeZone(tzid); // for desugar: call appropriate public TimeZone method
}
@GetMapping("/api/datetime")
public DateTime datetime() {
ZonedDateTime now = ZonedDateTime.now();
String date = now.format(DateTimeFormatter.ISO_LOCAL_DATE);
String time = now.format(DateTimeFormatter.ISO_LOCAL_TIME);
ZoneId zone = now.getZone();
ZoneOffset offset = zone.getRules().getOffset(now.toLocalDateTime());
String zoneString = String.format("%s (%s)",
zone.getDisplayName(FULL, Locale.UK), zone.getDisplayName(SHORT, Locale.UK));
return new DateTime(date, time, zoneString, zone.getId(), offset.getId());
}
@Override
public String marshal(ZoneId value) {
return value != null ? value.getId() : null;
}
public ZoneIdHandle(ZoneId o) {
this.zoneId = o.getId();
}
public static String toJson(@NonNull final ZoneId zoneId)
{
return zoneId.getId();
}
@Override
public String getAsText() {
ZoneId value = (ZoneId) getValue();
return (value != null ? value.getId() : "");
}
@Override
public JsonElement serialize(ZoneId value, Type type, JsonSerializationContext context) {
return value == null ? JsonNull.INSTANCE : new JsonPrimitive(value.getId());
}
@Override
public String getAsText() {
ZoneId value = (ZoneId) getValue();
return (value != null ? value.getId() : "");
}
public String toString(ZoneId value) {
return value.getId();
}
@Override
public String getAsText() {
ZoneId value = (ZoneId) getValue();
return (value != null ? value.getId() : "");
}
/**
* Create user visible label for given zone id.
*
* The created labels are in format $OFFSET $SHORT ($NAME), e.g.
*
* +01:00 CET (Europe/Prague)
* -07:00 PST (America/Los_Angeles)
*/
// @VisibleForTesting
static String getLabelForTimeZoneId(ZoneId zoneId) {
ZoneOffset zos = LocalDateTime.now().atZone(zoneId).getOffset();
return zos.getId().replaceAll("Z", "+00:00") + " " + zoneId.getDisplayName(TextStyle.SHORT, Locale.getDefault()) + " (" + zoneId.getId() + ")";
}