Avatar of pothios
pothios asked on

comparing timestamp help

hello. im trying to compare 2 timestamp and then check if its pass 60 second. i tried out the following but it gave me a wrong result. did i do anything wrong?


/* inserting to database */
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
java.util.Date date = new java.util.Date();
String sql = "insert into record values('" + userid + "', '" + status + "', ";
sql +="to_date('" + dateFormat.format(date) + "', 'YYYY/MM/DD HH:MI:SS'))";
conn.executeUpdate(sql);
		
/* retrieving from db */
String sql = "select max(time) as prevTime from record where user_id='" +userid+ "'";
Date recDate = mifa.getDate(sql); // returns a time stamp from oracle db
 
/* calculate the time */
java.util.Date now;
long diff;
now = new java.util.Date();
diff = now.getTime() - recDate.getTime(); // difference in milliseconds
diff = diff / 1000; // difference in seconds;

Open in new window

Java

Avatar of undefined
Last Comment
CEHJ

8/22/2022 - Mon
CEHJ

What type is 'mifa'?
CEHJ

More efficient to set the date btw would be:
String sql = "insert into record values(?, ?, ?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, userid);
ps.setString(2, status);
ps.setTimestamp(3, new Timestamp(System.currentTimeMillis());
conn.executeUpdate(sql);

Open in new window

CEHJ

btw, if 'recDate' is of type java.sql.Date, it won't have any time info. You must use Timestamp
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER
pothios

Mifa = new Mifa()
mifa.getDate();

Mifa is a class that contains a method dat executes the sql. i guess my naming sucks lol
ASKER
pothios

i needto compare current time with the time stored in the database. what im trying to achieve here is to prevent user from posting tag within a minute time.
ASKER CERTIFIED SOLUTION
CEHJ

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
pothios

thanks it worked
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
CEHJ

:-)
CEHJ

btw, in my example before

>>conn.executeUpdate(sql);

should have been

ps.executeUpdate(sql);