技巧|JAVA中时间操作

在这里插入图片描述

前言

日期处理,在JDK1.8版本之后,有了很多改变,时间处理上在日常项目中使用比比较频繁,各个公司,各个项目组针对日期处理都有不同工具类处理,但是好多都是jdk1.8之前的版本为主,虽然在日常使用过程中不受影响,但是本着与时俱进,跟随潮流的趋势,还是需要进行一定更新;

基本概念

格林威治时间

摘自维基百科:
在这里插入图片描述

最后需要了解的是,这个是世界市区的起点;定义的时区为0时区;

北京时间

北京时间指的是东八区的时间,与格林威治时间相差八个小时,如果格林威治时间是1日0点,那么北京时间为1日8点;

时间戳

摘自百度百科:
在这里插入图片描述

回顾

首先针对JDK8以前的操作方式,对日期进行简单操作流程进行回顾一下:

  • 获取时间

    // 方式一
    Date date = new Date();
    System.out.println(date);
    // 方式二
    Calendar calendar = Calendar.getInstance();
    Date time = calendar.getTime();
    System.out.println(time);
    
  • 获取时间戳

    // 方式一
    Date date = new Date();
    long time1 = date.getTime();
    System.out.println(time1);
    // 方式二
    long time2 = System.currentTimeMillis();
    System.out.println(time2);
    // 方式三
    long timeInMillis = Calendar.getInstance().getTimeInMillis();
    System.out.println(timeInMillis);
    
  • 时间格式化

    // 时间格式
    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E Z D");
    System.out.println(sf.format(new Date()));  // 2019-10-10 15:06:05 星期四 +0800 283
    // 星期格式
    SimpleDateFormat sf2 = new SimpleDateFormat("E");
    System.out.println(sf2.format(new Date()));
    

    SimpleDateFormat构造函数中数据参数意义按照下面格式给出:

    字符含义示例
    yyyyy-2019
    MMM-10
    d月中天数dd-10
    D年中的天数283
    E星期几星期四
    H小时数(24小时制)HH-15
    h小时数(12小时制)hh-03
    m分钟数mm-06
    s秒数ss-05
    Z时区+0800
  • 时间转换

    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    //String转Date
    String str = "2019-10-10 15:06:05";
    System.out.println(sf.parse(str));
    //时间戳的字符串转Date
    String tsString = "1556788591462";
    //import java.sql
    Timestamp ts = new Timestamp(Long.parseLong(tsString));
    System.out.println(sf.format(ts));
    

    Tips:使用方法parse会有异常需要处理ParseException,继承Exception,需要人为进行处理;这里就要求字符串的格式必须与构造函数中给定一致,不然会报错;

JDK1.8

jdk1.8之后为了方便时间操作,新增三个与时间相关操作的类,并且都是线程安全的,可以降低代码在多线程下风险也可以极大的方便代码操作;

主要是针对下面三个类进行展开:

  • LocalDate 只包含日期,不包含时间,不可变类,且线程安全。
  • LocalTime 只包含时间,不包含日期,不可变类,且线程安全。
  • LocalDateTime 既包含了时间又包含了日期,不可变类,且线程安全。
  • 获取时间

    // 获取日期
    LocalDate localDate = LocalDate.now();
    System.out.println(localDate);
    // 获取时间
    LocalTime localTime = LocalTime.now();
    System.out.println(localTime);
    // 获取日期和时间
    LocalDateTime localDateTime = LocalDateTime.now();
    System.out.println(localDateTime);
    
  • 获取时间戳

    // 方式一
    long milli = Instant.now().toEpochMilli(); // 获取当前时间戳(精确到毫秒)
    // 方式二
    long second = Instant.now().getEpochSecond(); // 获取当前时间戳(精确到秒)
    System.out.println(milli);  // output:1565932435792
    System.out.println(second); // output:1565932435
    
  • 时间格式化

    // 方式一
    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.
      								      ofPattern("yyyy-MM-dd HH:mm:ss");
    String timeFormat = dateTimeFormatter.format(LocalDateTime.now());
    System.out.println(timeFormat); // 2019-10-10 15:38:00
    // 方式二
    DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.
      									   ofPattern("yyyy-MM-dd HH:mm:ss");
    String timeFormat2 = LocalDateTime.now().format(dateTimeFormatter1);
    System.out.println(timeFormat2);// 2019-10-10 15:38:00
    
  • 时间转换

    上面时间格式化已经将日期格式转化成String类型,主要就是将String转换成日期;

    String timeStr = "2019-10-10 15:38:00";
    LocalDateTime dateTime = LocalDateTime.
    						 parse(timeStr,DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    System.out.println(dateTime);
    

其他使用

  • 比较日期常用的手段

    方式1: 获取时间戳,然后计算差值,为正表示时间更大,为负表示时间更小;

    方式2:通过Date自带的方法before(),after(),equals()方式进行判定;

  • 计算日期间隔

    使用jdk1.8提供的类Period计算两个日期的间隔;

    LocalDate d1 = LocalDate.now();
    LocalDate d2 = d1.plusDays(2);
    Period period = Period.between(d1, d2);
    System.out.println(period.getDays());   //2
    

总结

日期操作是平时业务开发过程中,经常使用到基本操作,经常会遇到各种各样不同的使用场景,这里对此进行简单总结,日常的开发的过程中,掌握了这些基本用法,其他日期操作的过程中都可以通过各种转化达到应用场景;

关于我

Hello,我是球小爷,热爱生活,求学七年,工作三载,而今已快入而立之年,如果您觉得对您有帮助那就一切都有价值,赠人玫瑰,手有余香❤️.最后把我最真挚的祝福送给您及其家人,愿众生一生喜悦,一世安康!

附录

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值