我想添加日期格式(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/