Instant | private final long seconds; private final int nanos; | |
---|---|---|
Clock | public abstract ZoneId getZone(); public abstract Instant instant(); public long millis() | |
ZoneId-ZoneOffset | private final int totalSeconds; private final transient String id; //+hh:mm ZoneRules getRules(); | 通过id计算出totalSeconds |
ZoneId-ZoneRegion | private final String id; private final transient ZoneRules rules; ZoneRules getRules(); | 一个区域可能有多个时间偏移量,比如夏令营时间 |
LocalDate | private final int year; private final short month; private final short day; | 1、UTC时间的起始时间是1970年1月1日0时0分0秒 2、LocalDate ofEpochDay(long epochDay) :将时间戳计算年月日 3、带时区计算,加上时区偏移量 longepochSec=now.getEpochSecond() +offset.getTotalSeconds();// overflow caught later longepochDay=Math.floorDiv(epochSec,SECONDS_PER_DAY); returnLocalDate.ofEpochDay(epochDay); |
LocalTime | private final byte hour; private final byte minute; private final byte second; private final int nano; | 方式同LocalDate LocalTime.ofNanoOfDay() |
LocalDateTime | private final LocalDate date; private final LocalTime time; public String toString() { return date.toString() + 'T' + time.toString(); } | 创建LocalDateTime ,必须 :1 需要基准时间,2、需要时区信息 // 时间戳 到年月日 + offset public static LocalDateTime ofEpochSecond(long epochSecond, int nanoOfSecond, ZoneOffset offset) { Objects.requireNonNull(offset, "offset"); NANO_OF_SECOND.checkValidValue(nanoOfSecond); long localSecond = epochSecond + offset.getTotalSeconds(); // overflow caught later long localEpochDay = Math.floorDiv(localSecond, SECONDS_PER_DAY); int secsOfDay = (int)Math.floorMod(localSecond, SECONDS_PER_DAY); LocalDate date = LocalDate.ofEpochDay(localEpochDay); LocalTime time = LocalTime.ofNanoOfDay(secsOfDay NANOS_PER_SECOND nanoOfSecond); return new LocalDateTime(date, time); } // to ZonedDateTime ,默认情况 LocalDateTime 值不会变 public ZonedDateTime atZone(ZoneId zone) { return ZonedDateTime.of(this, zone); } // to offsetTime public OffsetDateTime atOffset(ZoneOffset offset) { return OffsetDateTime.of(this, offset); } //逆运算 年月日 --> 时间戳 -offset default Instant toInstant(ZoneOffset offset) { returnInstant.ofEpochSecond(toEpochSecond(offset), toLocalTime().getNano()); } |
ZonedDateTime | private final LocalDateTime dateTime; private final ZoneOffset offset; private final ZoneId zone; | // 1 Create: 会重新计算LocalDateTime public static ZonedDateTime ofLocal(LocalDateTime localDateTime, ZoneId zone, ZoneOffset preferredOffset) { Objects.requireNonNull(localDateTime, "localDateTime"); Objects.requireNonNull(zone, "zone"); if (zone instanceof ZoneOffset) { return new ZonedDateTime(localDateTime, (ZoneOffset) zone, zone); } ZoneRules rules = zone.getRules(); List<ZoneOffsetvalidOffsets = rules.getValidOffsets(localDateTime); ZoneOffset offset; if (validOffsets.size() == 1) { offset = validOffsets.get(0); } else if (validOffsets.size() == 0) { ZoneOffsetTransition trans = rules.getTransition(localDateTime); localDateTime = localDateTime.plusSeconds(trans.getDuration().getSeconds()); offset = trans.getOffsetAfter(); } else { if (preferredOffset != null && validOffsets.contains(preferredOffset)) { offset = preferredOffset; } else { offset = Objects.requireNonNull(validOffsets.get(0), "offset"); // protect against bad ZoneRules } } return new ZonedDateTime(localDateTime, offset, zone); } // 2 Create: 使用时间戳创建,LocalDateTime 会使用时间戳+偏移量来计算 private static ZonedDateTime create(long epochSecond, int nanoOfSecond, ZoneId zone) { ZoneRules rules = zone.getRules(); Instant instant = Instant.ofEpochSecond(epochSecond, nanoOfSecond); // TODO: rules should be queryable by epochSeconds ZoneOffset offset = rules.getOffset(instant); LocalDateTime ldt = LocalDateTime.ofEpochSecond(epochSecond, nanoOfSecond, offset); return new ZonedDateTime(ldt, offset, zone); } |
OffsetDateTime | 同ZonedDateTime,不会动态计算偏移量,使用固定偏移量计算 LocalDateTime | |
DateTimeFormatter | 1、将字符串字段解析到 对应的字段中 2、ZoneDateTime ,LocalDateTime 对字段查询构造自己实例,构造过程中不带有偏移量计算 |
JAVA | date.time
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 本文主要探讨和时区相关的概念,并以jdk8的Date/Time API为例,明确我们在处理和时区相关的问题时,应当...
- 本文目的:掌握 Java 中日期和时间常用 API 的使用。参考:Jakob Jenkov的英文教程Java Da...
- String 类的概述 String类: 代表 字符串。Java 程序中的所有字符串字面值(如 "abc" )...