本文介绍了在DateTime字段上添加H:M:S值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何添加时间:MINUTES:SECONDS值在DateTime字段?

How to add HOURS:MINUTES:SECONDS value on DateTime field?

例如,我想在我的字段中包含dimetime值添加(sum)与字符串值其中包含HH:MM:SECONDS中的值。

On example I want in my field which contains dimetime value to add (sum) with string value which contains value in HH:MM:SECONDS.

ReportDate是 2013-01-01 10:00:00
要添加的值是: 120:34:29

ReportDate is 2013-01-01 10:00:00Value to add is :120:34:29

所以结果应该是:$ $ c> 2013-01-06 10:34:00

So result should be: 2013-01-06 10:34:00

我尝试过:

GregorianCalendar c = new GregorianCalendar();
c.setTime(reportDate);
String toAdd='120:34:29';
c.add(Calendar.HOUR, toAdd);
java.util.Date targetFinish = c.getTime();
SystemOutPrintln(targetFinish.toString()); 

所以有问题的行是 c.add(Calendar.HOUR,toAdd);
如何添加这些值?我想在公历上做。并希望在SysOut中有新的价值;
谢谢

So problematic row is c.add(Calendar.HOUR, toAdd);How to add these value? I want to do it over Gregorian Calendar. And want to have in SysOut that new value;Thank you

推荐答案

GregorianCalendar c = new GregorianCalendar();
c.setTime(reportDate);

String[] toAdd = "120:34:29".split(":");

int hours = Integer.parseInt(toAdd[0]);
int mins = Integer.parseInt(toAdd[1]);
int secs = Integer.parseInt(toAdd[2]);

c.add(Calendar.SECOND, secs);
c.add(Calendar.MINUTE, mins);
c.add(Calendar.HOUR, hours);

java.util.Date targetFinish = c.getTime();

SystemOutPrintln(targetFinish.toString());

这篇关于在DateTime字段上添加H:M:S值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 19:47