时间戳与Date类型转换

本文主要包含的有:

  1. 时间戳与Date类型的相互转换
  2. 判断是否为同一天
  3. 日期格式化
  4. 给日期加上指定时长
  5. 得到指定时间节点的Date

1、时间戳转Date

注:以下的方法中很多常量和方法我都没有提取出来,正式项目中还是建议大家封装在时间处理类中,规范化操作


 
  1. public static void main(String[] args) {
  2. // 10位的秒级别的时间戳
  3. long time1 = 1527767665;
  4. String result1 = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss").format( new Date(time1 * 1000));
  5. System.out.println( "10位数的时间戳(秒)--->Date:" + result1);
  6. Date date1 = new Date(time1* 1000); //对应的就是时间戳对应的Date
  7. // 13位的秒级别的时间戳
  8. double time2 = 1515730332000d;
  9. String result2 = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss").format(time2);
  10. System.out.println( "13位数的时间戳(毫秒)--->Date:" + result2);
  11. }

 
  1. 10位数的时间戳(秒)--->Date: 2018- 05- 31 19: 54: 25
  2. 13位数的时间戳(毫秒)--->Date: 2018- 01- 12 12: 12: 12

尤其要注意上面10位的秒级别的时间戳时,不能用int来定义time1变量,否则会得到错误的结果:


 
  1. public static void main(String[] args) {
  2. // 10位的秒级别的时间戳
  3. int time1 = 1527767665; //错误做法
  4. String result1 = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss").format( new Date(time1 * 1000));
  5. System.out.println( "10位数的时间戳(秒)--->Date:" + result1);
  6. }
10位数的时间戳(秒)--->Date:1969-12-17 23:21:47
 

2、Date转时间戳


 
  1. public static void main(String[] args) {
  2. //获取指定时间的时间戳,除以1000说明得到的是秒级别的时间戳(10位)
  3. long time = ( new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss")).parse( "2018-06-30 20:00:00", new ParsePosition( 0)).getTime() / 1000;
  4. //获取时间戳
  5. long now1 = System.currentTimeMillis();
  6. long now2 = new Date().getTime();
  7. System.out.println( "获取指定时间的时间戳:" + time);
  8. System.out.println( "当前时间戳:" +now1);
  9. System.out.println( "当前时间戳:" +now2);
  10. }

 
  1. 获取指定时间的时间戳: 1530360000
  2. 当前时间戳: 1527769494340
  3. 当前时间戳: 1527769494340

3、格式化Date


 
  1. public static void main(String[] args) {
  2. //使用common-lang包下面的DateFormatUtils类
  3. String format1 = DateFormatUtils.format( new Date(), "yyyy-MM-dd HH:mm:ss");
  4. //使用最原始的SimpleDateFormat类
  5. String format2 = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss").format( new Date());
  6. System.out.println( "格式化时间1:" + format1);
  7. System.out.println( "格式化时间2:" + format2);
  8. }

 
  1. 格式化时间 1: 2018- 05- 31 20: 26: 49
  2. 格式化时间 2: 2018- 05- 31 20: 26: 49

DateFormatUtils是commons.lang3.time.DateFormatUtils下的,如果你的项目中没有,maven中引入下:


 
  1. <dependency>
  2. <groupId>org.apache.commons </groupId>
  3. <artifactId>commons-lang3 </artifactId>
  4. <version>3.6 </version>
  5. </dependency>

4、给日期加上指定时长

比如,给现在的时间加上12个小时,这个需求也很常见,例如我做过的某个秒杀接口要返回剩余秒杀时间给前端,那么我就直接计算(比如秒杀持续时间为12小时):秒杀(倒计时)截止时间=(当前时间+12H)即可。


 
  1. public static void main(String[] args) {
  2. //将指定日期加上固定时间,DateUtils还有其它添加分钟、小时、月份之类的方法api
  3. //使用到的是commons-lang包下面的DateUitls类
  4. Date date = DateUtils.addDays( new Date(), 10); //
  5. System.out.println( "当前时间为:"+DateFormatUtils.format( new Date(), "yyyy-MM-dd HH:mm:ss"));
  6. String format = DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss");
  7. System.out.println( "当前时间加上10天后:" + format);
  8. }

 
  1. 当前时间为: 2018- 05- 31 20: 31: 53
  2. 当前时间加上 10天后: 2018- 06- 10 20: 31: 53

DateUtils也是commons.lang3.time包下的,别忘了加maven依赖。


5、得到指定时间节点的日期时间


 
  1. public static void main(String[] args) throws ParseException {
  2. //得到指定日期
  3. String date = "2018-03-03 15:20:12";
  4. Date parse = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss").parse(date);
  5. System.out.println(parse);
  6. }

6、判断两个时间点是否为同一天、同一年

给定两个日期,快速判断两者是否为同一天或者同一年,比如我做过的一个接口需求是:每人每天有一次抽奖机会,那么当用户当天第二次发送请求时候,我就得判断查询参与记录,并且判断最新一次参与时间和当天是否为同一天。


 
  1. /**
  2. * 判断是否为同一天:使用commons-lang包下的DateUtils类
  3. *
  4. * @param day1
  5. * @param day2
  6. * @return
  7. */
  8. public boolean isSameDay(Date day1, Date day2) {
  9. return DateUtils.isSameDay(day1, day2);
  10. }
  11. /**
  12. * 判断是否为同一天:使用joda依赖包里的时间类,效率从一定程度上优于DateUtils.isSameDay()方法
  13. *
  14. * @param date1
  15. * @param date2
  16. * @return
  17. */
  18. public static boolean isSameDay1(Date date1,Date date2){
  19. if(date1== null || date2== null){
  20. throw new IllegalArgumentException( "date must be not null");
  21. }
  22. LocalDate localDate1 = new LocalDate( new DateTime(date1.getTime()));
  23. LocalDate localDate2 = new LocalDate( new DateTime(date2.getTime()));
  24. return localDate1.equals(localDate2);
  25. }

基本上涉及到时间的处理就这么多了,希望对你有参考帮助!


books 引申阅读: 使用quartz实现高级定制化定时任务(包含管理界面)

books 推荐阅读:elastic search搜索引擎实战demo:https://github.com/simonsfan/springboot-quartz-demo,分支:feature_es


转载来源:https://blog.youkuaiyun.com/fanrenxiang/article/details/80531649

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值