时间戳 - 数据的创建时间距离当前时间的日期
条件:数据库中存的时间数据是时间戳(例如: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