首页 > 其他 > 详细

时间戳 - 数据的创建时间距离当前时间的日期

时间:2020-11-16 15:59:06      阅读:37      评论:0      收藏:0      [点我收藏+]

时间戳 - 数据的创建时间距离当前时间的日期

 

条件:数据库中存的时间数据是时间戳(例如:1605510434   这是秒级的时间戳,毫秒级的是13位的);

需求: 计算用户创建时间距今多少天;

 

// 从数据中获取创建时间的时间戳(到秒级别的,如果到毫秒的话下方代码需要变动)
Integer create_time = Integer.parseInt(user_info.get("create_time").toString());
// 当前时间戳 - 年月日时分秒格式的( .getTime() 方法获取的是毫秒级的时间戳 )
String timestamp = String.valueOf((new Date()).getTime());
// 若是数据库中存的数据是毫秒级的,那么下面的两行代码就不要再写了,下面两行的代码就是通过截取倒数三位数据之前的数据,把毫秒级的时间数据变成秒级的
int length = timestamp.length();
Integer date = Integer.valueOf(timestamp.substring(0, length - 3));
// 若是毫秒级的数据进行计算的话,把 (3600 * 24) 变成 (1000 * 3600 * 24)
int days = (int) ((date - create_time) / (3600*24));
System.out.println(days);

 

时间戳 - 数据的创建时间距离当前时间的日期

原文:https://www.cnblogs.com/90s-ITBoy/p/13985208.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!