[软件设计/软件工程] 添加格式为 dd:HH:mm:ss 的日期

[复制链接]
发表于 2022-5-5 09:15:53
问题
我有三个日期作为字符串对象,格式为:dd:HH:mm:ss

00:1:9:14

00:3:10:4

00:3:39:49

如何在 Java 中添加这些日期以获得总和( 00:7:59:07 )?

示例代码:
  1. SimpleDateFormat sdf = new SimpleDateFormat("dd:HH:mm:ss");
  2. Date d1 = sdf.parse("00:1:9:14");
  3. Date d2 = sdf.parse("00:3:10:4");
  4. Date d3 = sdf.parse("00:3:39:49");

  5. System.out.println(d1);
  6. System.out.println(d2);
  7. System.out.println(d3);
  8. Date d = new Date(d1.getTime() + d2.getTime() + d3.getTime());

  9. System.out.println(d);
复制代码

输出(错误):
  1. Wed Dec 31 01:09:14 IST 1969
  2. Wed Dec 31 03:10:04 IST 1969
  3. Wed Dec 31 03:39:49 IST 1969
  4. Sun Dec 28 20:59:07 IST 1969
复制代码

回答
dd 格式包括月份中的日期。因此,如果您使用 00 (或 Java SimpleDateFormat ,因为它还包括月份中的哪一天),您的 Date 值将下溢。相反,分析您的时间部分并自己进行计算。

例如,您可以使用 TimePart 、 days 、 hours 、 minutes 和 seconds 创建一个类
  1. static class TimePart {
  2.     int days = 0;
  3.     int hours = 0;
  4.     int minutes = 0;
  5.     int seconds = 0;

  6.     static TimePart parse(String in) {
  7.         if (in != null) {
  8.             String[] arr = in.split(":");
  9.             TimePart tp = new TimePart();
  10.             tp.days = ((arr.length >= 1) ? Integer.parseInt(arr[0]) : 0);
  11.             tp.hours = ((arr.length >= 2) ? Integer.parseInt(arr[1]) : 0);
  12.             tp.minutes = ((arr.length >= 3) ? Integer.parseInt(arr[2]) : 0);
  13.             tp.seconds = ((arr.length >= 4) ? Integer.parseInt(arr[3]) : 0);
  14.             return tp;
  15.         }
  16.         return null;
  17.     }

  18.     public TimePart add(TimePart a) {
  19.         this.seconds += a.seconds;
  20.         int of = 0;
  21.         while (this.seconds >= 60) {
  22.             of++;
  23.             this.seconds -= 60;
  24.         }
  25.         this.minutes += a.minutes + of;
  26.         of = 0;
  27.         while (this.minutes >= 60) {
  28.             of++;
  29.             this.minutes -= 60;
  30.         }
  31.         this.hours += a.hours + of;
  32.         of = 0;
  33.         while (this.hours >= 24) {
  34.             of++;
  35.             this.hours -= 24;
  36.         }
  37.         this.days += a.days + of;
  38.         return this;
  39.     }

  40.     @Override
  41.     public String toString() {
  42.         return String.format("%02d:%02d:%02d:%02d", days, hours, minutes,
  43.                 seconds);
  44.     }
  45. }
复制代码

那么你的测试用例
  1. public static void main(String[] args) {
  2.     try {
  3.         TimePart d1 = TimePart.parse("00:1:9:14");
  4.         TimePart d2 = TimePart.parse("00:3:10:4");
  5.         TimePart d3 = TimePart.parse("00:3:39:49");
  6.         System.out.println(d1);
  7.         System.out.println(d2);
  8.         System.out.println(d3);
  9.         TimePart d4 = d1.add(d2).add(d3);
  10.         System.out.println(d4);
  11.     } catch (Exception e) {
  12.         e.printStackTrace();
  13.     }
  14. }
复制代码

它似乎正确地执行了加法,比如
  1. 00:01:09:14
  2. 00:03:10:04
  3. 00:03:39:49
  4. 00:07:59:07
复制代码






上一篇:使用 JWT 管理多个设备节点 js 的用户会话
下一篇:将数据帧写入 Postgres 数据库 psycop2

使用道具 举报

Archiver|手机版|小黑屋|吾爱开源 |网站地图

Copyright 2011 - 2012 Lnqq.NET.All Rights Reserved( ICP备案粤ICP备14042591号-1粤ICP14042591号 )

关于本站 - 版权申明 - 侵删联系 - Ln Studio! - 广告联系

本站资源来自互联网,仅供用户测试使用,相关版权归原作者所有

快速回复 返回顶部 返回列表