本文整理了Java中org.threeten.bp.ZonedDateTime
类的一些代码示例,展示了ZonedDateTime
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ZonedDateTime
类的具体详情如下:
包路径:org.threeten.bp.ZonedDateTime
类名称:ZonedDateTime
[英]A date-time with a time-zone in the ISO-8601 calendar system, such as 2007-12-23T10:15:30+01:00 Europe/Paris.
ZonedDateTime is an immutable representation of a date-time with a time-zone. This class stores all date and time fields, to a precision of nanoseconds, and a time-zone, with a zone offset used to handle ambiguous local date-times. For example, the value "2nd October 2007 at 13:45.30.123456789 +02:00 in the Europe/Paris time-zone" can be stored in a ZonedDateTime.
This class handles conversion from the local time-line of LocalDateTimeto the instant time-line of Instant. The difference between the two time-lines is the offset from UTC/Greenwich, represented by a ZoneOffset.
Converting between the two time-lines involves calculating the offset using the ZoneRules accessed from the ZoneId. Obtaining the offset for an instant is simple, as there is exactly one valid offset for each instant. By contrast, obtaining the offset for a local date-time is not straightforward. There are three cases:
Any method that converts directly or implicitly from a local date-time to an instant by obtaining the offset has the potential to be complicated.
For Gaps, the general strategy is that if the local date-time falls in the middle of a Gap, then the resulting zoned date-time will have a local date-time shifted forwards by the length of the Gap, resulting in a date-time in the later offset, typically "summer" time.
For Overlaps, the general strategy is that if the local date-time falls in the middle of an Overlap, then the previous offset will be retained. If there is no previous offset, or the previous offset is invalid, then the earlier offset is used, typically "summer" time.. Two additional methods, #withEarlierOffsetAtOverlap() and #withLaterOffsetAtOverlap(), help manage the case of an overlap.
A ZonedDateTime holds state equivalent to three separate objects, a LocalDateTime, a ZoneId and the resolved ZoneOffset. The offset and local date-time are used to define an instant when necessary. The zone ID is used to obtain the rules for how and when the offset changes. The offset cannot be freely set, as the zone controls which offsets are valid.
This class is immutable and thread-safe.
[中]ISO-8601日历系统中带有时区的日期时间,例如2007-12-23T10:15:30+01:00 Europe/Paris。
ZoneDateTime是带有时区的日期时间的不可变表示形式。此类存储所有日期和时间字段(精度为纳秒)以及时区,时区偏移用于处理不明确的本地日期时间。例如,值“2007年10月2日欧洲/巴黎时区13:45.30.123456789+02:00”可以存储在ZoneDateTime中。
此类处理从LocalDateTime的本地时间线到instant的即时时间线的转换。两条时间线之间的差异是UTC/格林威治的偏移量,用区域偏移表示。
在两条时间线之间转换涉及使用从ZoneId访问的ZoneRules计算偏移量。获取一个瞬间的偏移量很简单,因为每个瞬间只有一个有效偏移量。相比之下,获取本地日期时间的偏移量并不简单。有三种情况:
*正常,有一个有效偏移。对于一年中的绝大多数时间,正常情况适用,当地日期和时间只有一个有效的偏移量。
*间隙,具有零有效偏移。这通常是由于春季夏令时从“冬季”变为“夏季”,时钟向前跳的时候。在间隙中,存在没有有效偏移的本地日期时间值。
*重叠,具有两个有效偏移。这是因为秋季夏令时从“夏季”变为“冬季”,时钟通常会被延迟。在重叠中,有两个有效偏移的本地日期时间值。
任何通过获取偏移量将本地日期时间直接或隐式转换为瞬间的方法都有可能变得复杂。
对于间隙,一般的策略是,如果本地日期时间落在间隙的中间,则所产生的分区日期时间将具有由间隙的长度向前移动的本地日期时间,从而导致稍后的偏移中的日期时间,通常为“夏季”时间。
对于重叠,一般策略是,如果本地日期时间落在重叠的中间,则将保留先前的偏移。如果没有以前的偏移量,或者以前的偏移量无效,则使用以前的偏移量,通常是“夏季”时间。。另外两种方法,#WithEarlierOffsetToVerlap()和#WithLaterOffsetToVerlap()有助于管理重叠情况。
####实施者规范
ZoneDateTime保存的状态相当于三个单独的对象:LocalDateTime、ZoneId和解析的ZoneOffset。偏移量和本地日期时间用于在必要时定义一个瞬间。分区ID用于获取偏移如何以及何时更改的规则。无法自由设置偏移,因为分区控制哪些偏移是有效的。
这个类是不可变的,是线程安全的。
代码示例来源:origin: ThreeTen/threetenbp
@Override
public ZonedDateTime queryFrom(TemporalAccessor temporal) {
return ZonedDateTime.from(temporal);
}
};
代码示例来源:origin: ThreeTen/threetenbp
/**
* Resolves the new local date-time using the offset to identify the instant.
*
* @param newDateTime the new local date-time, not null
* @return the zoned date-time, not null
*/
private ZonedDateTime resolveInstant(LocalDateTime newDateTime) {
return ofInstant(newDateTime, offset, zone);
}
代码示例来源:origin: mercadolibre/java-sdk
@Override
public ZonedDateTime apply(ZonedDateTime zonedDateTime, ZoneId zoneId) {
return zonedDateTime.withZoneSameInstant(zoneId);
}
}
代码示例来源:origin: com.github.joschi.jackson/jackson-datatype-threetenbp
@Override
public long applyAsLong(ZonedDateTime dt) {
return dt.toInstant().toEpochMilli();
}
},
代码示例来源:origin: ThreeTen/threetenbp
ZonedDateTime end = ZonedDateTime.from(endExclusive);
if (unit instanceof ChronoUnit) {
end = end.withZoneSameInstant(zone);
if (unit.isDateBased()) {
return dateTime.until(end.dateTime, unit);
} else {
return toOffsetDateTime().until(end.toOffsetDateTime(), unit);
代码示例来源:origin: ThreeTen/threetenbp
/**
* Converts a {@code ZonedDateTime} to a {@code Calendar}.
* <p>
* The resulting {@code GregorianCalendar} is pure Gregorian and uses
* ISO week definitions, starting on Monday and with 4 days in a minimal week.
* <p>
* Fractions of the instant smaller than milliseconds will be dropped.
*
* @param zdt the zoned date-time, not null
* @return the calendar, not null
* @throws IllegalArgumentException if the conversion fails
*/
public static GregorianCalendar toGregorianCalendar(ZonedDateTime zdt) {
TimeZone zone = toTimeZone(zdt.getZone());
GregorianCalendar cal = new GregorianCalendar(zone);
cal.setGregorianChange(new Date(Long.MIN_VALUE));
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.setMinimalDaysInFirstWeek(4);
try {
cal.setTimeInMillis(zdt.toInstant().toEpochMilli());
} catch (ArithmeticException ex) {
throw new IllegalArgumentException(ex);
}
return cal;
}
代码示例来源:origin: ThreeTen/threetenbp
ChronoField f = (ChronoField) field;
switch (f) {
case INSTANT_SECONDS: return create(newValue, getNano(), zone);
case OFFSET_SECONDS: {
ZoneOffset offset = ZoneOffset.ofTotalSeconds(f.checkValidIntValue(newValue));
return resolveOffset(offset);
return resolveLocal(dateTime.with(field, newValue));
代码示例来源:origin: ThreeTen/threetenbp
/**
* Converts this date-time to a {@code ZonedDateTime} using the offset as the zone ID.
* <p>
* This creates the simplest possible {@code ZonedDateTime} using the offset
* as the zone ID.
* <p>
* To control the time-zone used, see {@link #atZoneSameInstant(ZoneId)} and
* {@link #atZoneSimilarLocal(ZoneId)}.
*
* @return a zoned date-time representing the same local date-time and offset, not null
*/
public ZonedDateTime toZonedDateTime() {
return ZonedDateTime.of(dateTime, offset);
}
代码示例来源:origin: ThreeTen/threetenbp
if (field instanceof ChronoField) {
switch ((ChronoField) field) {
case INSTANT_SECONDS: return toEpochSecond();
case OFFSET_SECONDS: return getOffset().getTotalSeconds();
代码示例来源:origin: ThreeTen/threetenbp
long epochSecond = temporal.getLong(INSTANT_SECONDS);
int nanoOfSecond = temporal.get(NANO_OF_SECOND);
return create(epochSecond, nanoOfSecond, zone);
return of(ldt, zone);
} catch (DateTimeException ex) {
throw new DateTimeException("Unable to obtain ZonedDateTime from TemporalAccessor: " +
代码示例来源:origin: ThreeTen/threetenbp
/**
* Obtains an instance of {@code ZonedDateTime} from a text string such as
* {@code 2007-12-23T10:15:30+01:00[Europe/Paris]}.
* <p>
* The string must represent a valid date-time and is parsed using
* {@link org.threeten.bp.format.DateTimeFormatter#ISO_ZONED_DATE_TIME}.
*
* @param text the text to parse such as "2007-12-23T10:15:30+01:00[Europe/Paris]", not null
* @return the parsed zoned date-time, not null
* @throws DateTimeParseException if the text cannot be parsed
*/
public static ZonedDateTime parse(CharSequence text) {
return parse(text, DateTimeFormatter.ISO_ZONED_DATE_TIME);
}
代码示例来源:origin: XeroAPI/Xero-Java
public LocalDate deserialize(JsonParser jsonparser, DeserializationContext context)
throws IOException, JsonProcessingException {
String date = jsonparser.getText();
LocalDate formattedDate;
Pattern datePatt = Pattern.compile("^/Date\\((\\d+)([+-]\\d+)?\\)/$");
Matcher m = datePatt.matcher(date);
if (m.matches()) {
Long l = Long.parseLong(m.group(1));
formattedDate = Instant.ofEpochMilli(l).atZone(ZoneId.systemDefault()).toLocalDate();
} else {
throw new IllegalArgumentException("Wrong date format");
}
return formattedDate;
}
}
代码示例来源:origin: com.github.joschi.jackson/jackson-datatype-threetenbp
@Override
public long applyAsLong(ZonedDateTime dt) {
return dt.toEpochSecond();
}
},
代码示例来源:origin: com.github.joschi.jackson/jackson-datatype-threetenbp
@Override
public int applyAsInt(ZonedDateTime dt) {
return dt.getNano();
}
},
代码示例来源:origin: Ullink/simple-slack-api
private String convertDateToSlackTimestamp(ZonedDateTime date) {
return (date.toInstant().toEpochMilli() / 1000) + ".123456";
}
代码示例来源:origin: org.threeten/threetenbp
ZonedDateTime end = ZonedDateTime.from(endExclusive);
if (unit instanceof ChronoUnit) {
end = end.withZoneSameInstant(zone);
if (unit.isDateBased()) {
return dateTime.until(end.dateTime, unit);
} else {
return toOffsetDateTime().until(end.toOffsetDateTime(), unit);
代码示例来源:origin: org.threeten/threetenbp
/**
* Converts a {@code ZonedDateTime} to a {@code Calendar}.
* <p>
* The resulting {@code GregorianCalendar} is pure Gregorian and uses
* ISO week definitions, starting on Monday and with 4 days in a minimal week.
* <p>
* Fractions of the instant smaller than milliseconds will be dropped.
*
* @param zdt the zoned date-time, not null
* @return the calendar, not null
* @throws IllegalArgumentException if the conversion fails
*/
public static GregorianCalendar toGregorianCalendar(ZonedDateTime zdt) {
TimeZone zone = toTimeZone(zdt.getZone());
GregorianCalendar cal = new GregorianCalendar(zone);
cal.setGregorianChange(new Date(Long.MIN_VALUE));
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.setMinimalDaysInFirstWeek(4);
try {
cal.setTimeInMillis(zdt.toInstant().toEpochMilli());
} catch (ArithmeticException ex) {
throw new IllegalArgumentException(ex);
}
return cal;
}
代码示例来源:origin: org.threeten/threetenbp
ChronoField f = (ChronoField) field;
switch (f) {
case INSTANT_SECONDS: return create(newValue, getNano(), zone);
case OFFSET_SECONDS: {
ZoneOffset offset = ZoneOffset.ofTotalSeconds(f.checkValidIntValue(newValue));
return resolveOffset(offset);
return resolveLocal(dateTime.with(field, newValue));
代码示例来源:origin: org.threeten/threetenbp
/**
* Converts this date-time to a {@code ZonedDateTime} using the offset as the zone ID.
* <p>
* This creates the simplest possible {@code ZonedDateTime} using the offset
* as the zone ID.
* <p>
* To control the time-zone used, see {@link #atZoneSameInstant(ZoneId)} and
* {@link #atZoneSimilarLocal(ZoneId)}.
*
* @return a zoned date-time representing the same local date-time and offset, not null
*/
public ZonedDateTime toZonedDateTime() {
return ZonedDateTime.of(dateTime, offset);
}
代码示例来源:origin: org.threeten/threetenbp
if (field instanceof ChronoField) {
switch ((ChronoField) field) {
case INSTANT_SECONDS: return toEpochSecond();
case OFFSET_SECONDS: return getOffset().getTotalSeconds();
内容来源于网络,如有侵权,请联系作者删除!