Solved

Add 2 hours to current timestamp in Java

Posted on 2012-04-02
6
320 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

739 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