java 8新特性 instant 和 LocalDateTime

Instant

 java.time 包中的 Instant 类代表的是某个时间(有点像 java.util.Date),准确的说是:”是不带时区的即时时间点“,它是精确到纳秒的(而不是象旧版本的Date精确到毫秒)。如果使用纳秒去表示一个时间则原来使用一位Long类型是不够的,需要占用更多一点的存储空间,实际上其内部是由两个Long字段组成,第一个部分保存的是自标准Java计算时代(就是1970年1月1日开始)到现在的秒数,第二部分保存的是纳秒数(永远不会超过999,999,999)。

实例代码如下:

   public static void main(String[] args) {
      //instant 相当于 date
        Instant instant = Instant.now();
        System.out.println(instant);
        Date date = new Date();
        System.out.println(date);
        //instant转date 类方法(java.util.date)
        Date from = Date.from(instant);
        System.out.println(date);
        //datet转instant 对象方法(java.util.date)
        Instant instant1 = date.toInstant();
        System.out.println(instant1);
        //instant 根据毫秒值或者date转换为instant 类方法 (java.time)
        Instant instant2 = Instant.ofEpochMilli(date.getTime());
        System.out.println(instant2);
        //instant 根据秒值或者date转换为instant 类方法 (java.time)
        Instant instant3 = Instant.ofEpochSecond(60 * 60L);
        Instant instant4 = Instant.ofEpochSecond(60 * 60 * 24L);
        System.out.println(instant3);
        System.out.println(instant4);
        //instant 第一个参数指定秒数,第二个单位指定纳秒数,这样得到的instant会包含纳秒的数据 1000000000纳秒(9位)=1秒
        Instant instant5 = Instant.ofEpochSecond(60 * 60 * 24L,  1000000000*60L);
        System.out.println(instant5);
        //instant 将字符串类型的instant转换为instantd对象,如:1970-01-02T00:01:00Z,但是date的结果字符串不可以转换,会报错DateTimeParseException
        //注意:必须传入的是符合 UTC格式的字符串
        Instant parse = Instant.parse("1970-01-01T01:00:00Z");
        System.out.println(parse+"parse");
        //instant 在现有的instant的时间上追加些时间,下面例子追加了5小时10分钟,这里plus会产生新的instant对象
        Instant plus = instant.plus(Duration.ofHours(5).plusMinutes(10));
        System.out.println(instant+" instant "+plus+"plus");
        System.out.println(instant == plus);//plus会产生新的instant对象 所以结果位false
        //instant 获取其5天前的instant(此刻)
        Instant minus = instant.minus(5, ChronoUnit.HOURS);
        System.out.println(instant+" instant "+minus+" minus");
        //也可以直接调用相关减法方法,效果跟上面的方法一样
        Instant instant6 = instant.minusSeconds(60 * 60 * 5);
        System.out.println(instant+" instant "+instant6+" instant6");
        //减法方法,效果跟上面的方法一样
        Instant minus1 = instant.minus(Duration.ofHours(5));
        System.out.println(instant+" instant "+minus1+" minus1");
        //计算两个Instant之间的秒数, ChronoUnit用的什么,得到的结果就是什么单位
        System.out.println(instant+" instant "+instant6+" instant6");
        long between = ChronoUnit.SECONDS.between(instant6, instant);
        System.out.println(between);
        //比较两个instant 相等 0, 前者时间纳秒值大于后者 1,小于后者 -1或小于0
        int i = instant.compareTo(instant6);
        System.out.println(i);
        //判断instant时间前后,前者在后者之后返回true,反之false
        boolean after = instant.isAfter(instant6);
        System.out.println(after);
        //判断instant时间前后,前者在后者之前返回true,反之false,正好与上面相反
        boolean before = instant.isBefore(instant6);
        System.out.println(before);


    }

说明:Java.time 这个包是线程安全的,所以可以替换之前的date类的使用。

注意:

Instant now = Instant.now();
System.out.println("now:"+now);

控制台输出:now:2018-07-09T08:59:08.853Z

通过这种方式获取的时间戳与北京时间相差8个时区,需要修正为北京时间,通过查看源代码发现Instant.now()使用等是UTC时间Clock.systemUTC().instant()。LocalDate、LocalDateTime 的now()方法使用的是系统默认时区 不存在Instant.now()的时间问题。

解决方法
如果要对应北京时间,需要增加8个小时

Instant now = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(8));
System.out.println("now:"+now);

控制台输出:now:2018-07-09T16:58:48.188Z
 

LocalDateTime

它表示的是不带时区的 日期及时间,替换之前的Calendar。看上去,LocalDateTime和Instant很象,但记得的是“Instant中是不带时区的即时时间点。可能有人说,即时的时间点 不就是日期+时间么?看上去是这样的,但还是有所区别,比如LocalDateTime对于用户来说,可能就只是一个简单的日期和时间的概念,考虑如下的 例子:两个人都在2013年7月2日11点出生,第一个人是在英国出生,而第二个是在加尼福利亚,如果我们问他们是在什么时候出生的话,则他们看上去都是 在同样的时间出生(就是LocalDateTime所表达的),但如果我们根据时间线(如格林威治时间线)去仔细考察,则会发现在出生的人会比在英国出生的人稍微晚几个小时(这就是Instant所表达的概念,并且要将其转换为UTC格式的时间)。

实例代码如下:

   public static void main(String[] args) {
        //LocalDateTime 相当于calendar
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime+" localDateTime1");
        //当前时间加上5小时,分钟等一样的用法,支持链式编程
        LocalDateTime localDateTime1 = localDateTime.plusHours(5);
        System.out.println(localDateTime1+" localDateTime1");
        //当前时间加上5小时,分钟等一样的用法,支持链式编程 但是这里localtime只是时间,不展示年月日,只展示如:15:26:50.398 时分秒毫秒
        LocalTime localDateTime2 = localDateTime.toLocalTime().plusHours(5);
       //当前时间加上5天,只展示年月日,不展示时分秒毫秒,下面是两种写法,都可以
        LocalDate localDate = localDateTime.toLocalDate().plusDays(5);
        System.out.println(localDateTime2+" localDateTime2 "+localDate+ " localDate");
        LocalDate plus = localDateTime.toLocalDate().plus(Period.ofDays(5));
        System.out.println(plus+"  plus");


    }

 

格式化日期

 LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.now() 
 ,ZoneId.systemDefault());

 String format = localDateTime.format(DateTimeFormatter.ofPattern("yyyy:MM:dd HH:mm:ss"));
 System.out.println(format);

另一种方式使用会报错:

Instant now = Instant.now();
   
String format = DateTimeFormatter.ofPattern("yyyy:MM:dd HH:mm:ss").format(now);

system.out.println(format);

格式化日期并获取这一周内的数据

 LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.now() 
 ,ZoneId.systemDefault());

String createStartTp =localDateTime.plusDays(-6).format(DateTimeFormatter.ofPattern("yyyy/MM/dd 00:00:00"));

system.out.println(createStartTp);

 

报错信息:

java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: YearOfEra
at java.time.Instant.getLong(Instant.java:608)

 

文章借鉴处:

https://www.cnblogs.com/sbj-dawn/p/7439953.html

https://blog.csdn.net/chunzhilianxue/article/details/80974202

  • 6
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值