在java8以前,做有关时间日期的操作时使用java.util.Date,做日期、月份、天数相加减操作时使用java.util.Calendar,对时间日期进行格式化操作时,使用SimpleDateFormat或DateFormat下的其他子类;但是这些时间日期操作对象,都是可变的、线程不安全的。
java8出的新的时间日期java.timeAPI都是线程安全的,并且性能更好,代码更简洁!
ZoneId: 时区ID,用来确定Instant和LocalDateTime互相转换的规则
//获得本地默认时区
ZoneId defaultZoneId = ZoneId.systemDefault();
Instant instant=Instant.now();
System.out.println(defaultZoneId+"-------"+instant);
LocalDate localDate = instant.atZone(defaultZoneId).toLocalDate();
LocalDateTime localDateTime = instant.atZone(defaultZoneId).toLocalDateTime();
System.out.println(localDate+"-------"+localDateTime);
Instant: 用来表示时间线上的一个点(瞬时),表示一个时间戳。Instant可以精确到纳秒,这超过了long的最大表示范围,所以在Instant的实现中是分成了两部分来表示,一部分是seconds,表示从1970-01-01 00:00:00开始到现在的秒数,另一个部分是nanos,表示纳秒部分。
//默认获取UTC时区,相对于GMT晚了八个小时
Instant instant=Instant.now();
System.out.println(System.currentTimeMillis());
System.out.println("秒:"+instant.getEpochSecond()+"----毫秒:"+instant.toEpochMilli());
System.out.println(instant);
OffsetDateTime odt = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(odt);
LocalDate、LocalTime、LocalDateTime类的实例是不可变的并且线程安全的对象,分别使用了ISO-8601系统日期、时间、日期和时间。它们提供了简单的日期或时间,并不包含当前的时间信息,也不包含与时区相关的信息。
//获取当前时间
LocalDateTime ldt=LocalDateTime.now();
System.out.println(ldt);
//获取指定时间
LocalDateTime ldt2=LocalDateTime.of(2020,06,02,11,11,11);
System.out.println(ldt2);
//对时间进行加减操作,都会产生一个新的日期对象,不会改变原来的日期对象
//加2年
LocalDateTime ldt3 = ldt.plusYears(2);
System.out.println(ldt3);
//减5天
LocalDateTime ldt4 = ldt.minusDays(5);
System.out.println(ldt4);
//获取年月日
System.out.println("year:"+ldt.getYear());
System.out.println("month:"+ldt.getMonth()+"--"+ldt.getMonthValue());
System.out.println("day:"+ldt.getDayOfMonth());
System.out.println("hour:"+ldt.getHour()+" minute:"+ldt.getMinute()+" second:"+ldt.getSecond());
//当前日期
LocalDate ld1= LocalDate.now();
System.out.println(ld1);
//当前时间
LocalTime lt1=LocalTime.now();
System.out.println(lt1);
Clock: 用于访问当前时刻、日期、时间,用到时区
//获取当前clock
Clock clock=Clock.systemUTC();
//通过Clock获取当前时刻
System.out.println("当前时刻为: "+clock.instant());
//获取clock对应的毫秒数,与system.currentTimeMillis()输出相同
System.out.println(clock.millis());
System.out.println(System.currentTimeMillis());
System.out.println(Instant.now().toEpochMilli());
Duration: 用秒和纳秒表示时间的数量(长短),用于计算两个日期的“时间”间隔
//Duration 计算两个时间的间隔
Instant instant1=Instant.now();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Instant instant2=Instant.now();
System.out.println(instant2.toEpochMilli()-instant1.toEpochMilli());
Duration duration1 = Duration.between(instant1, instant2);
System.out.println(duration1.getSeconds()+"--"+duration1.toMillis());
System.out.println("------------------------------");
LocalTime lt1=LocalTime.now();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
LocalTime lt2= LocalTime.now();
Duration duration2 = Duration.between(lt1, lt2);
System.out.println(duration2.getSeconds()+"--"+duration2.toMillis());
System.out.println("*********************");
LocalDateTime ldt1= LocalDateTime.now();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
LocalDateTime ldt2=LocalDateTime.now();
Duration duration3 = Duration.between(ldt1, ldt2);
System.out.println(duration3.getSeconds()+"--"+duration3.toMillis());
Period: 用于计算两个“日期”间隔
//Period 计算日期的间隔
LocalDate ld1=LocalDate.of(2019,04,22);
LocalDate ld2= LocalDate.now();
Period period1 = Period.between(ld1, ld2);
System.out.println(ld1+"---------"+ld2);
System.out.println(period1);
System.out.println("相差年月日:"+period1.getYears()+"年"+period1.getMonths()+"月"+period1.getDays()+"日");
System.out.println("相差天数为:"+(ld2.toEpochDay()-ld1.toEpochDay()));
TemPoralAdjuster 时间校正器
//TemPoralAdjuster 时间校正器,
LocalDateTime ldt1=LocalDateTime.now();
System.out.println(ldt1);
//指定这个月的5号
LocalDateTime ldt2 = ldt1.withDayOfMonth(5);
System.out.println(ldt2);
//下个星期天 TemporalAdjusters通过静态方法提供了大量的常用TemporalAdjuster的实现
LocalDateTime ldt3 = ldt1.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
System.out.println(ldt3);
//自定义,当前时间加7天
LocalDateTime ldt5 = ldt1.with((l) -> {
LocalDateTime ldt4 = (LocalDateTime) l;
return ldt4.plusDays(7);
});
System.out.println(ldt5);
DateTimeFormatter 格式化时间/日期
//DateTimeFormatter 格式化时间/日期
LocalDateTime ldt1=LocalDateTime.now();
DateTimeFormatter dtf=DateTimeFormatter.ISO_DATE_TIME;
String strTime=ldt1.format(dtf);
System.out.println(strTime);
//java8以前时间格式化
Date date=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(date));
//自定义格式
//把日期时间格式化为字符串
DateTimeFormatter dtf2= DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
String strTime2=ldt1.format(dtf2);
System.out.println(strTime2);
//把字符串日期格式化为日期时间
LocalDateTime ldt2=LocalDateTime.parse(strTime2,dtf2);
System.out.println(ldt2);
ZonedDate、ZonedTime、ZonedDateTime 带时区的时间
//获取所有时区
Set availableZoneIds = ZoneId.getAvailableZoneIds();
availableZoneIds.forEach(System.out::println);
LocalDateTime ldt1=LocalDateTime.now(ZoneId.of("Asia/Singapore"));
System.out.println(ldt1);
LocalDateTime ldt2=LocalDateTime.now();
System.out.println(ldt2);
ZonedDateTime zdt1 = ldt2.atZone(ZoneId.of("Asia/Shanghai"));
System.out.println(zdt1);