gpt4 book ai didi

java - 添加迄今为止的数字(H :mm:ss:SSS) format

转载 作者:行者123 更新时间:2023-12-01 18:05:28 25 4
gpt4 key购买 nike

我想添加日期格式(H:mm:ss:SSS)。我做了以下操作-

    Date time1;
Date time2;
String result1;
SimpleDateFormat formatter;
formatter = new SimpleDateFormat("H:mm:ss:SSS");
time1 = new Date(0,0,0,0,0,5);
time2=new Date(0,0,0,0,0,5);

time1 的输出是0:00:05:000。现在我想添加这两次并生成 0:00:10:000。但 time1+time2 是不可能的。有什么办法可以做到这一点吗?

最佳答案

您可以使用日历来满足您的要求:

    Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.set(Calendar.HOUR_OF_DAY,0);
calendar.set(Calendar.MINUTE,0);
calendar.set(Calendar.SECOND,5);
calendar.set(Calendar.MILLISECOND,0);

System.out.println(formatter.format(calendar.getTime()));

Calendar another = Calendar.getInstance();
another.setTime(calendar.getTime());
another.set(Calendar.HOUR_OF_DAY,0);
another.set(Calendar.MINUTE,0);
another.set(Calendar.SECOND,calendar.get(Calendar.SECOND)+5);
another.set(Calendar.MILLISECOND,0);

System.out.println(formatter.format(another.getTime()));

输出:

0:00:05:000
0:00:10:000

这是 Calendar 的 Java 文档

关于java - 添加迄今为止的数字(H :mm:ss:SSS) format,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36812690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com