Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Add 2 hours to current timestamp in Java

Posted on 2012-04-02
6
Medium Priority
?
354 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
5 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 1000 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 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.
Suggested Courses

810 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