java8的time类
- Instant
- LocalDate和LocalTime
- LocalDateTime
- DateTimeFormatter
Instant
Instant类代表某个时间,精确到纳秒。如果使用纳秒去表示一个时间则原来使用以为Long类型是不够的。需要占用更多一点的存储空间,实际上其内部是由两个Long字段组成,第一部分是标准Java计算时代(1970.1.1)开始到现在的秒数,第二部分保存的是纳秒(永远不会超过999,999,999)
public static void instantTest() {
//获取当前时间
Instant instant = Instant.now();
System.out.println("instant:" + instant);
//加5分钟
Instant instant1 = instant.plus(Duration.ofMinutes(5));
System.out.println("instant1:" + instant1);
//加五小时五分钟
Instant plus = instant1.plus(Duration.ofHours(5).plusMinutes(5));
System.out.println("plus:" + plus);
//减去天
Instant minus = instant.minus(Duration.ofDays(5));
System.out.println("minus:" + minus);
//计算两个时间的分钟数,instant1-instant
long between = ChronoUnit.MINUTES.between(instant, instant1);
System.out.println("between:" + between);
//instant1和instant进行比较,instant1>instant为1,反之为-1
//%n是换行
System.out.format("instant1.compareTo(instant)=%d%n", instant1.compareTo(instant));
//isBefore():instant1在instant之前,isAfter():instant1在instant之后
System.out.format("instant1.isAfter(instant)=%b,instant1.isBefore(instant)=%b.%n", instant1.isAfter(instant), instant1.isBefore(instant));
}
LocalDate和LocalTime
LocalDate表示不带时区的日期,比如1-1-2022
LocalTime表示不带时区的时间,比如04:45:50:12
public static void localDateAndLocalTimeTest() {
//获取LocalDate实例
LocalDate localDate = LocalDate.now();
System.out.println("localDate:" + localDate);
//获得2022年的第65天;2022-03-06
LocalDate localDate1 = LocalDate.ofYearDay(2022, 65);
System.out.println("localDate1:" + localDate1);
//2022-12-10
LocalDate localDate2 = LocalDate.of(2022, Month.DECEMBER, 10);
System.out.println("localDate2:" + localDate2);
//获取LocalTime的实例
LocalTime localTime = LocalTime.now();
System.out.println("localTime:" + localTime);
//返回一天中的第4503秒,01:15:03
LocalTime localTime1 = LocalTime.ofSecondOfDay(4503);
System.out.println("localTime1:" + localTime1);
//22:33
LocalTime localTime2 = LocalTime.of(22, 33);
System.out.println("localTime2:" + localTime2);
//计算的API与Instant一样
}
LocalDateTime
最后来看下在简单日期和时间类中最重要的一个:LocalDataTeime。它是LocalDate和LocalTime的组合体,表示的是不带时区的日期及时
间。看上去,LocalDateTime和Instant很象,但记得的是"Instant中是不带时区的即时时间点。可能有人说,即时的时间点不就是日期+时间
么?看上去是这样的,但还是有所区别,比如LocalDateTime对于用户来说,可能就只是一个简单的日期和时间的概念,考虑如下的例子:
两个人都在2013年7月2日11点出生,第一个人是在英国出生,而第二个是在加尼福利亚,如果我们问他们是在什么时候出生的话,则他们
看上去都是在同样的时间出生(就是LocalDateTime所表达的),但如果我们根据时间线(如格林威治时间线)去仔细考察,则会发现在出
生的人会比在英国出生的人稍微晚几个小时(这就是Instant所表达的概念,并且要将其转换为UTC格式的时间)。除此之
外,LocalDateTime的用法和上述介绍的其他类都很相似,如下例子所示:
public static void localDateTimeTest() {
LocalDateTime localDateTime =LocalDateTime.now();
System.out.println("localDateTime:"+localDateTime);
//当前时间加上20小时5分钟
LocalDateTime localDateTime1 = localDateTime.plusHours(20).plusMinutes(5);
System.out.println("localDateTime1:"+localDateTime1);
//转为LocalDate,加上10天
LocalDate localDate = localDateTime1.toLocalDate().plusDays(10);
System.out.println("localDate:"+localDate);
//转为LocalTime,加上10小时5分钟
LocalTime localTime = localDateTime1.toLocalTime().plus(Duration.ofHours(10).plusMinutes(5));
System.out.println("localTime:"+localTime);
}
DateTimeFormatter
DateTimeFormatter是线程安全的,SimpleDateFormat不是线程安全的,阿里开发手册推荐使用DateTimeFormatter替换掉SimpleDateFormat
使用SimpleDateFormat类进行格式化存在一些缺点:
1、每次处理时间都会new一个SimpleDateFormat实例化对象,会占用大量的内存和jvm空间
2、为减少内存开销,会将其使用static修饰,变为共享变量,单手SimpleDateFormat是线程不安全的,需要加锁的方式
来解决,java8后使用DateTimeFormatter来代替SimpleDateFormat
public static void dateTimeFormatter() {
String dateStr="2021-06-01 12:00:08";
String dateStr1="2021年06月01日";
//字符串转日期
//通过传入格式化字符串实现DateTimeFormatter实例的创建
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime localDateTime = LocalDateTime.parse(dateStr, dtf);
System.out.println(localDateTime);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
LocalDate date = LocalDate.parse(dateStr1, formatter);
System.out.println(date);
//日期转字符串
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
String format = now.format(timeFormatter);
System.out.println(format);
}
Instant:瞬时实例。
LocalDate:本地⽇期,不包含具体时间。
LocalTime:本地时间,不包含⽇期。
LocalDateTime:组合了⽇期和时间,但不包含时差和时区信息。
ZonedDateTime:最完整的⽇期时间,包含时区和相对UTC或格林威治的时差。
//Date转LocalDateTime
Instant instant = date.toInstant();
ZoneId zoneId = ZoneId.systemDefault();
Date date = instant.atZone(zoneId).toLocalDateTime();
//LocalDateTime转Date
ZoneId zoneId = ZoneId.systemDefault();
ZonedDateTime zdt = localDateTime.atZone(zoneId);
LocalDateTime localDateTime = Date.from(zdt.toInstant()