How do I add time to a timestamp?

主宰稳场 提交于 2019-12-04 04:06:54

问题


I need to add 14 minutes and 59 seconds to an unknown time in an array. How do I do this? This is what I have so far:

Date duration = df.parse("0000-00-00 00:14:59");

arrayOpportunity[2] = arrayOpportunity[2] + duration;

The time is not being changed. Thanks!

I have done my research. I cant paste the entire code I have. But mainly I didnt want to make you read it all. Just looking for a simple answer of how to add two timestamps.


回答1:


If you are talking about a java.sql.Timestamp, it has a method called setTime. java.util.Date has a setTime method as well for that sort of thing.

You could something like this:

static final Long duration = ((14 * 60) + 59) * 1000;

oldTimestamp.setTime(oldTimestamp.getTime() + duration);



回答2:


If you want to add time in millis then you can just add

  (((14 * 60) + 59) * 1000) <-- Mili second value of 14 m and 59 sec



回答3:


Just add the appropriate number of milliseconds using #getTime() and #setTime():

timeStamp.setTime(timeStamp.getTime() + (((14 * 60) + 59)* 1000));



回答4:


If you just want to add times, I suggest using Joda Time.

The class LocalTime lets you add durations like this:

LocalTime timeSum = time.plusMinutes(14).plusSeconds(59);



回答5:


arrayOpportunity[2] = arrayOpportunity[2] + 14*60*1000 + 59*1000;

The Date object you have may work, but it doesn't really represent 14 minutes and 59 seconds, it just represents a particular time in calendar (eg. 14 minutes 59 after the epoch start which is 1st January 1970 00:14:59).



来源:https://stackoverflow.com/questions/13051298/how-do-i-add-time-to-a-timestamp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!