Solved

Add 2 hours to current timestamp in Java

Posted on 2012-04-02
6
281 Views
Last Modified: 2013-01-25
Hi everyone,

Little weak in this area...

Im running a scheduler job using Java 1.5 and I pulling the record from the database, then getting the create timestamp of the record from the oracle database and then I want to add 2 hours to it and see if greater than the current time.  Basically Im delaying processing of a record until two hours have gone by.

Could some provide me a simple example of adding 2 hours to the timestamp and then checking against the current timestamp to see if it needs processed.
0
Comment
Question by:TheVeee
6 Comments
 
LVL 15

Expert Comment

by:Ess Kay
ID: 37798190
you can use some sql

here is some info
http://stackoverflow.com/questions/257324/oracle-how-to-add-minutes-to-a-timestamp

or use java


Just compute the minutes and the timestamp as milliseconds and add them.
long t=theOldTimeStamp.getTime();
long m=minutes*60*1000

TimeStamp theNewTimestamp=new TimeStamp (t+m);

Open in new window

0
 
LVL 26

Accepted Solution

by:
ksivananth earned 250 total points
ID: 37799514
the above one ignores nano secs, try this,

long time = ts.getTime() ;
time = time + ( 2 * 60 * 60 * 1000 ) ;
TimeStamp newTs = new TimeStamp( time ) ;
newTs.setNanos( ts.getNanos() ) ;
0
 
LVL 12

Expert Comment

by:Anuradha Goli
ID: 37806321
compute the hours as millisecs and assign as timestamp

(2*60*60*1000)+your current time
0
 
LVL 5

Assisted Solution

by:knsp
knsp earned 250 total points
ID: 37896365
DateFormat formatter = new SimpleDateFormat("HH:mm:ss");  
 
try {  
    Date start = formatter.parse("09:11:59");  
    Long startMilli = start.getTime();          
    Long resultMilli = startMilli + 7200000; // adding 2 hrs in miliseconds      
    Date result = new Date(resultMilli);  
    System.out.println(formatter.format(result));    
} catch (ParseException ex) {  
    ex.printStackTrace();  
}
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 38818066
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now