java8-日期时间Api
目录
- LocalDate
- 更新时间
- LocalTime
- LocalDateTime
- Instant
- Period Duration
- 格式化、解析日期-时间对象
- 时区
java.util.Date
java.util.Calendar
不支持时区
线程不安全
月份从0起
线程不安全,只有包裹在ThreadLocal中才安全
java.text.DateFormat
java.text.SimpleDateFormat
LocalDate 2020-01-13
LocalTime 16:39:09.307
LocalDateTime 2020-01-13T16:40:59.138
Instant 时间戳类
Period 计算 2 个日期(LocalDate,年月日)之间的差距
Duration 计算 2 个时间(LocalTime,时分秒)之间的差距
ZonedDateTime 包含时区的时间
LocalDate date = LocalDate.parse("2014-03-18");
LocalTime time = LocalTime.parse("13:45:20");
LocalDate
LocalDate date = LocalDate.of(2014, 3, 18); 2014-03-18
int year = date.getYear(); 2014
Month month = date.getMonth(); MARCH
int day = date.getDayOfMonth(); 18
DayOfWeek dow = date.getDayOfWeek();TUESDAY
int len = date.lengthOfMonth(); 31
boolean leap = date.isLeapYear(); 是否为闰年 false
LocalDate today = LocalDate.now(); 从系统时钟中获取当前时间
int year = date.get(ChronoField.YEAR);
int month = date.get(ChronoField.MONTH_OF_YEAR);
int day = date.get(ChronoField.DAY_OF_MONTH);
更新时间
直接修改
LocalDate date2 = date1.withYear(2011);
LocalDate date3 = date2.withDayOfMonth(25);
LocalDate date4 = date3.with(ChronoField.MONTH_OF_YEAR, 9);
加上减去时间
LocalDate date2 = date1.plusWeeks(1);
LocalDate date3 = date2.minusYears(3);
LocalDate date4 = date3.plus(6, ChronoUnit.MONTHS);
LocalDate 、 LocalTime 、 LocalDateTime 以及 Instant
import static java.time.temporal.TemporalAdjusters.*;
LocalDate date1 = LocalDate.of(2014, 3, 18); 2014-03-18
LocalDate date2 = date1.with(nextOrSame(DayOfWeek.SUNDAY)); 2014-03-23
LocalDate date3 = date2.with(lastDayOfMonth()); 2014-03-31
LocalTime
LocalTime time = LocalTime.of(13, 45, 20); 13:45:20
int hour = time.getHour(); 13
int minute = time.getMinute(); 45
int second = time.getSecond(); 20
LocalDateTime
// 2014-03-18T13:45:20
LocalDateTime dt1 = LocalDateTime.of(2014, Month.MARCH, 18, 13, 45, 20);
LocalDate date1 = dt1.toLocalDate(); 2014-03-18
LocalTime time1 = dt1.toLocalTime(); 13:45:20
LocalDate date = LocalDate.parse("2014-03-18");
LocalTime time = LocalTime.parse("13:45:20");
LocalDateTime dt2 = LocalDateTime.of(date, time);
LocalDateTime dt3 = date.atTime(13, 45, 20);
LocalDateTime dt4 = date.atTime(time);
LocalDateTime dt5 = time.atDate(date);
Instant
Instant 以Unix元年时间(传统的设定为UTC时区1970年1月1日午夜时分)开始所经历的
秒数进行计算
ofEpochSecond 静态方法
Instant.ofEpochSecond(3);
Instant.ofEpochSecond(3, 0);
Instant.ofEpochSecond(2, 1_000_000_000); 2秒后加上100万纳秒(1秒)
Instant.ofEpochSecond(4, -1_000_000_000); 4秒前的100万纳秒(1秒)
Period Duration
Period 计算 2 个日期(LocalDate,年月日)之间的差距
Duration 计算 2 个日期 时分秒 之间的差距
Duration threeMinutes = Duration.ofMinutes(3);
Duration threeMinutes = Duration.of(3, ChronoUnit.MINUTES);
Period tenDays = Period.ofDays(10);
Period threeWeeks = Period.ofWeeks(3);
Period twoYearsSixMonthsOneDay = Period.of(2, 6, 1);
Duration d1 = Duration.between(time1, time2);
Duration d1 = Duration.between(dateTime1, dateTime2);
Duration d2 = Duration.between(instant1, instant2);
Period tenDays = Period.between(LocalDate.of(2014, 3, 8),
LocalDate.of(2014, 3, 18));
格式化、解析日期-时间对象
LocalDate date = LocalDate.of(2014, 3, 18);
String s1 = date.format(DateTimeFormatter.BASIC_ISO_DATE); 20140318
String s2 = date.format(DateTimeFormatter.ISO_LOCAL_DATE); 2014-03-18
LocalDate date1 = LocalDate.parse("20140318", DateTimeFormatter.BASIC_ISO_DATE);
LocalDate date2 = LocalDate.parse("2014-03-18", DateTimeFormatter.ISO_LOCAL_DATE);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate date1 = LocalDate.of(2014, 3, 18);
String formattedDate = date1.format(formatter);
LocalDate date2 = LocalDate.parse(formattedDate, formatter);
意大利时区
DateTimeFormatter italianFormatter = DateTimeFormatter.ofPattern("d. MMMM yyyy", Locale.ITALIAN);
LocalDate date1 = LocalDate.of(2014, 3, 18);
String formattedDate = date.format(italianFormatter); // 18. marzo 2014
LocalDate date2 = LocalDate.parse(formattedDate, italianFormatter);
DateTimeFormatter italianFormatter = new DateTimeFormatterBuilder()
.appendText(ChronoField.DAY_OF_MONTH)
.appendLiteral(". ")
.appendText(ChronoField.MONTH_OF_YEAR)
.appendLiteral(" ")
.appendText(ChronoField.YEAR)
.parseCaseInsensitive()
.toFormatter(Locale.ITALIAN);
时区
新的 java.time.ZoneId 类是老版 java.util.TimeZone 的替代品
ZoneId romeZone = ZoneId.of("Europe/Rome");
{区域}/{城市}
老的时区对象转换为 ZoneId
ZoneId zoneId = TimeZone.getDefault().toZoneId();
LocalDate date = LocalDate.of(2014, Month.MARCH, 18);
ZonedDateTime zdt1 = date.atStartOfDay(romeZone);
LocalDateTime dateTime = LocalDateTime.of(2014, Month.MARCH, 18, 13, 45);
ZonedDateTime zdt2 = dateTime.atZone(romeZone);
Instant instant = Instant.now();
ZonedDateTime zdt3 = instant.atZone(romeZone);
LocalDateTime 转换为 Instant:
LocalDateTime dateTime = LocalDateTime.of(2014, Month.MARCH, 18, 13, 45);
Instant instantFromDateTime = dateTime.toInstant(romeZone);
Instant 转换为 LocalDateTime:
Instant instant = Instant.now();
LocalDateTime timeFromInstant = LocalDateTime.ofInstant(instant, romeZone);
表达时区的第二种方式:利用当前时区和UTC/格林尼治的固定偏差
当前时间和伦敦格林尼治子午线时间的差异
ZoneOffset newYorkOffset = ZoneOffset.of("-05:00");
对于UTC/格林尼治时间的偏差方式表示日期时间
LocalDateTime dateTime = LocalDateTime.of(2014, Month.MARCH, 18, 13, 45);
OffsetDateTime dateTimeInNewYork = OffsetDateTime.of(date, newYorkOffset);
ISO-8601日历系统是世界文明日历系统的事实标准
4种其他的日历系统
ThaiBuddhistDate(泰国佛教日历系统)、MinguoDate(民国日期)、 JapaneseDate(日本日历)、HijrahDate(伊斯兰教日历)
LocalDate date = LocalDate.of(2014, Month.MARCH, 18);
JapaneseDate japaneseDate = JapaneseDate.from(date);
Chronology japaneseChronology = Chronology.ofLocale(Locale.JAPAN);
ChronoLocalDate now = japaneseChronology.dateNow();
日期及时间API的设计者建议我们使用 LocalDate ,尽量避免使用 ChronoLocalDate
存储、操作、业务规则的解读使用 LocalDate
如果你需要将程序的输入或者输出本地化,这时你应该使用ChronoLocalDate 类