joda-time jar包,如果jdk不是8版本也可以用新的日期时间
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.9</version>
一、LocalDate、LocalTime、LocalDateTime
now(): 获取当前时间,当前时区默认
of:获取指定时间
with:指定时间
plus:增加
minus:减少
get:获取
@Test
public void test01() {
LocalDateTime ldt = LocalDateTime.now();
System.out.println(ldt);
LocalDateTime ldt2 = LocalDateTime.of(2019, 8, 15, 22, 9, 59);
System.out.println(ldt2);
LocalDateTime ldt3 = ldt.plusYears(2);
System.out.println(ldt3);
LocalDateTime ldt4 = ldt.minusYears(2);
System.out.println(ldt4);
System.out.println(ldt.getYear());
System.out.println(ldt.getDayOfMonth());
System.out.println(ldt.getDayOfWeek().getValue());
System.out.println(ldt.getDayOfYear());
System.out.println(ldt.getHour());
System.out.println(ldt.getMinute());
System.out.println(ldt.getMonth());
System.out.println(ldt.getSecond());
System.out.println(ldt.getNano());
}
二、 Instant : 时间戳。 (使用 Unix 元年 1970年1月1日 00:00:00 所经历的毫秒值)
now():获取当前时间,返回默认UTC时区的Instant对象
atOffset:设置时区偏移量
@Test
public void test02() {
Instant it = Instant.now();
System.out.println(it);
//设置偏移量
OffsetDateTime time = it.atOffset(ZoneOffset.ofHours(8));
System.out.println(time);
//获取精确到秒
long epochSecond = it.getEpochSecond();
System.out.println(epochSecond);
//获取精确到毫秒
long l = it.toEpochMilli();
System.out.println(l);
//获取指定60秒的时间:距离0970 年0 分0秒
Instant instant = Instant.ofEpochSecond(60);
System.out.println(instant);
}
三、计算两个日期之间的间隔
Duration : 用于计算两个“时间”间隔
Period : 用于计算两个“日期”间隔
@Test
public void test03() throws InterruptedException {
Instant it01 = Instant.now();
Thread.sleep(1000);
Instant it02 = Instant.now();
//两个时间的间隔
Duration between = Duration.between(it01, it02);
System.out.println(between.toMillis());
System.out.println("----------------------------");
LocalDateTime ldt1 = LocalDateTime.now();
Thread.sleep(1000);
LocalDateTime ldt2 = LocalDateTime.now();
System.out.println(Duration.between(ldt1,ldt2).toMillis());
}
//计算两个日期之间的间隔
@Test
public void test04() {
LocalDate ld = LocalDate.of(2018, 4, 16);
LocalDate ld2 = LocalDate.now();
Period period = Period.between(ld, ld2);
System.out.println(period);
System.out.println(period.getYears());
System.out.println(period.getMonths());
System.out.println(period.getDays());
}
四、TemporalAdjuster : 时间校正器
TemporalAdjusters:提供时间矫正器工具
@Test
public void test05() {
//当前时间
LocalDateTime ldt = LocalDateTime.now();
System.out.println(ldt);
//指定这个月的第十天
LocalDateTime localDateTime = ldt.withDayOfMonth(10);
System.out.println(localDateTime);
//指定这一年的第一天
LocalDateTime with = ldt.with(TemporalAdjusters.firstDayOfYear());
System.out.println(with);
//自定义,下一个工作日
LocalDateTime with1 = ldt.with(e -> {
LocalDateTime localDateTime1 = (LocalDateTime) e;
DayOfWeek day = localDateTime1.getDayOfWeek();
System.out.println(day);
if (day.equals(DayOfWeek.FRIDAY)) {
return localDateTime1.plusDays(3);
} else if (day.equals(DayOfWeek.SATURDAY)) {
return localDateTime1.plusDays(2);
} else {
return localDateTime1.plusDays(1);
}
});
System.out.println(with1);
}
五、DateTimeFormatter
解析和格式化日期或时间 多种方式调用格式化,LocalDateTime 或者DateTimeFormatter的方法都可以
@Test
public void test06() {
DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE;
LocalDateTime ldt = LocalDateTime.now();
System.out.println(ldt);
//LocalDateTime的方法格式化
String format = ldt.format(dtf);
System.out.println(format);
System.out.println("------------------------");
DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss E");
//DateTimeFormatter 的格式化方法
String format1 = dtf2.format(ldt);
System.out.println(format1);
LocalDateTime parse = LocalDateTime.parse(format1, dtf2);
System.out.println(parse);
}
/**
* DateTimeFormatter
*/
@Test
public void test03() {
DateTimeFormatter isoDateTime = DateTimeFormatter.ISO_DATE_TIME;
// datetime 适用于 SHORT LONG MEDIUM
DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
// date 适用于 FULL SHORT LONG MEDIUM
DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG);
DateTimeFormatter formatter3 = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//格式化
String format = formatter.format(LocalDateTime.now());
System.out.println(format);
TemporalAccessor parse = formatter.parse(format);
System.out.println(parse);
String format1 = formatter1.format(LocalDateTime.now());
System.out.println(format1);
}
六 、ZonedDate、ZonedTime、ZonedDateTime : 带时区的时间或日期
ZoneId
@Test
public void test07() {
//获取所有支持的时区
Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
availableZoneIds.forEach(System.out::println);
//创建日期的时候指定时区
LocalDateTime now = LocalDateTime.now(ZoneId.of("US/Pacific"));
System.out.println(now);
System.out.println("____________________________");
LocalDateTime now1 = LocalDateTime.now();
ZonedDateTime atZone = now1.atZone(ZoneId.of("Europe/Athens"));
System.out.println(atZone);
System.out.println("____________________________");
LocalDateTime now2 = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
ZonedDateTime zonedDateTime = now2.atZone(ZoneId.of("Europe/Tallinn"));//这个时间还是now2的时间,但是后面会多个时区并且标明跟UTC相差几个时区
System.out.println(now2);
System.out.println(zonedDateTime);
//2019-08-16T00:12:05.965
//2019-08-16T00:12:05.965+03:00[Europe/Tallinn]
}
@Test
public void test05() {
Set<String> availableZoneIds =
ZoneId.getAvailableZoneIds();
int size = availableZoneIds.size();
System.out.println(size);
// availableZoneIds.forEach(System.out::println);
LocalDateTime now = LocalDateTime.now(ZoneId.of("US/Pacific"));
System.out.println(now);
ZonedDateTime zonedDateTime = now.atZone(ZoneId.of("Asia/Shanghai"));
System.out.println(now);
System.out.println(zonedDateTime);
// 2020-10-08T23:12:06.269
// 2020-10-08T23:12:06.269
// 2020-10-08T23:12:06.269+08:00[Asia/Shanghai] 这个时间还是now的时间,但是后面会多个时区并且标明跟UTC相差几个时区
}
7 Java8 LocalDateTime和Date相互转换
Date to LocalDateTime
Date todayDate = new Date();
LocalDateTime ldt = todayDate.toInstant()
.atZone( ZoneId.systemDefault() )
.toLocalDateTime();
System.out.println(ldt);
//2019-05-16T19:22:12.773
LocalDateTime to Date
LocalDateTime localDateTime = LocalDateTime.now();
Date date = Date.from( localDateTime.atZone( ZoneId.systemDefault()).toInstant());
System.out.println(date);
//Thu May 16 19:22:37 CST 2019