Java8日期处理

日期处理相关的类的含义

类名 含义
LocalDate 不含时区信息的日期
LocalTime 不含时区信息的时间
LocalDateTime 不含时区信息的日期时间
Instant 不含时区信息的时间戳
ZoneId 一个时区
ZonedDateTime 含时区信息的日期时间
Period 以年、月、日描述的时间区间
Duration 以秒、纳秒描述的时间区间
Clock 含时区信息的时钟

日期时间

创建一个表示日期时间的实例

LocalDate localDate = LocalDate.now();
LocalDate localDate = LocalDate.of(2018, 8, 8);
LocalDate localDate = LocalDate.parse("2018-08-08");

LocalTime localTime = LocalTime.now();
LocalTime localTime = LocalTime.of(8, 0, 0);
LocalTime localTime = LocalTime.parse("08:00:00");

LocalDateTime localDateTime = LocalDateTime.now();
LocalDateTime localDateTime = LocalDateTime.of(2018,8,8,8,0,0);
LocalDateTime localDateTime = LocalDateTime.parse("2018-08-08T08:00:00");

日期时间调整

localDateTime = localDateTime.withYear(2017);
localDateTime = localDateTime.withMonth(11);
localDateTime = localDateTime.withDayOfMonth(11);
localDateTime = localDateTime.withHour(11);
localDateTime = localDateTime.with(TemporalAdjusters.lastDayOfMonth());

日期时间运算

localDateTime = localDateTime.plusMonths(1);
localDateTime = localDateTime.minusSeconds(1);

日期时间分解

int year = localDateTime.getYear();
int monthValue = localDateTime.getMonthValue();
int dayOfMonth = localDateTime.getDayOfMonth();
int hour = localDateTime.getHour();
int minute = localDateTime.getMinute();
int second = localDateTime.getSecond();

先后比较

boolean result = localDateTime.isBefore(localDateTime2);
boolean result = localDateTime.isAfter(localDateTime2);

日期格式化与解析

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime localDateTime = LocalDateTime.parse("2018-08-08 08:00:00", dateTimeFormatter);
String localDateTimeString = localDateTime.format(dateTimeFormatter);

计算差距

long result = ChronoUnit.DAYS.between(localDateTime, localDateTime2);

时间戳

创建一个表示时间戳的实例

Instant instant = Instant.now();
Instant instant = Instant.ofEpochMilli(1468568760);

时间戳实例转毫秒数

long epochMilli = instant.toEpochMilli();

时区

创建一个表示时区的实例

ZoneId zoneId = ZoneId.systemDefault();
ZoneId zoneId = ZoneId.of("GMT+8");
ZoneId zoneId = ZoneId.of("UTC+8");
ZoneId zoneId = ZoneId.of("Asia/Shanghai");
ZoneId zoneId = ZoneId.of("+8");

创建一个带时区信息的日期时间

ZonedDateTime zonedDateTime = ZonedDateTime.now();
ZonedDateTime zonedDateTime = ZonedDateTime.of(2016, 2, 19, 14, 52, 0, 0, ZoneId.of("GMT+8"));

转换时区

// 日期和时间会根据时区的差值改变,也会处理夏令时的问题
ZonedDateTime zonedDateTime = zonedDateTime.withZoneSameInstant(ZoneId.of("GMT+1"));

时间区间

创建一个表示时间区间的实例

// 1年1月1日的时间区间
Period period = Period.of(1, 1, 1);
Period period = Period.parse("P1Y1M1D");

// 100秒的时间区间
Duration duration = Duration.ofSeconds(100);
// 1天1小时1分钟1.1秒的时间区间
Duration duration = Duration.parse("P1DT1H1M1.1S");

时间区间运算

period = period.plusDays(1);
duration = duration.minusSeconds(1);

计算时间区间

Period period = Period.between(localDate, localDate2);
Duration duration = Duration.between(localDateTime, localDateTime2);

时钟

创建一个表示时钟的实例

Clock clock = Clock.systemDefaultZone();
Clock clock = Clock.system(zoneId);

调整时钟

// 时钟时间在原时间之后
clock = Clock.offSet(clock, Duration.ofHours(1));
// 时钟时间在原时间之前
clock = Clock.offSet(clock, Duration.ofHours(-1));

时钟跳动

// 每整数秒跳动一次
clock = Clock.tick(clock, Duration.ofSeconds(1));

获取时钟的毫秒数

long millis = clock.millis();

类型转换

LocalDateTime转LocalDate

LocalDate localDate = localDateTime.toLocalDate();

LocalDateTime转LocalTime

LocalTime localTime = localDateTime.toLocalTime();

LocalDate和LocalTime转LocalDateTime

LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);

Instant转LocalDateTime

LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zoneId);

Instant转ZonedDateTime

ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, zoneId);

LocalDateTime转ZonedDateTime

ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId);

ZonedDateTime转Instant

Instant instant = zonedDateTime.toInstant();

Clock转Instant

Instant instant = clock.instant();

旧类型转换

Date转Instant

Instant instant = date.toInstant();

Instant转Date

Date date = Date.from(instant);