java 判断时间 间隔天数

阳光穿透心脏的1/2处 2021-09-26 11:16 485阅读 0赞
  1. import java.text.SimpleDateFormat;
  2. import java.util.Date;
  3. public class TimeUtil {
  4. public TimeUtil() {
  5. // TODO Auto-generated constructor stub
  6. }
  7. public static void main(String[] args){
  8. long startTime = 1516636799;
  9. long endTime = 1516636800;
  10. System.out.println(equation(startTime * 1000, System.currentTimeMillis()));
  11. System.out.println(System.currentTimeMillis());
  12. }
  13. //计算两个时间戳间隔多少天
  14. public static int equation(long startTime, long endTime) {
  15. startTime = dateToStamp(stampToDate(startTime));
  16. endTime = dateToStamp(stampToDate(endTime));
  17. int newL = (int) ((endTime - startTime) / (1000 * 3600 * 24));
  18. return newL;
  19. }
  20. /* * 将时间戳转换为时间 */
  21. public static String stampToDate(long l) {
  22. String res;
  23. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
  24. long lt = l;
  25. Date date = new Date(lt);
  26. res = simpleDateFormat.format(date);
  27. return res;
  28. }
  29. /* * 将时间转换为时间戳 */
  30. public static long dateToStamp(String s) {
  31. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
  32. Date date = null;
  33. try {
  34. date = simpleDateFormat.parse(s);
  35. return date.getTime();
  36. } catch (java.text.ParseException e) {
  37. // TODO Auto-generated catch block
  38. e.printStackTrace();
  39. return -1;
  40. }
  41. }
  42. }

发表评论

表情:
评论列表 (有 0 条评论,485人围观)

还没有评论,来说两句吧...

相关阅读

    相关 java时间间隔

    Problem Description 从键盘输入两个时间点(24小时制),输出两个时间点之间的时间间隔,时间间隔用“小时:分钟:秒”表示。 如:3点5分25秒应表示