Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Difference between 2 dates in milliseconds 1

Status
Not open for further replies.

BabyJeffy

Programmer
Sep 10, 2003
4,189
GB
Hello all.

I'm more a JSP scripter than a fully fledged Java developer. This has lead to a rather blinkered view of Java - and, as hilighted in this case, I am struggling to get to grips with some rather simplistic code requirements. My current role does not give me the chance to develop my Java any further at work.

I'm successfully creating a new calendar and getting out the milliseconds "since the epoch"... converting this into a String and then assigning this to a cookie (so I have a string representation of a long). On another page I want to compare the difference between the current milliseconds and the milliseconds stored in my cookie. I can get the value from the cookie and store it in a String... no problems. After that... I'm stuck!

Initially I had attempted to just convert both to a Long and then subtract one from the other... but I found that I was unable to do a simple subtraction (Delta = Long - Long) - I got all kinds of errors. So I moved on to thinking I would create 2 date objects and then compare them (since I had the initial milliseconds as a string, and I could easily coerce the current milliseconds into another string). I then ran into so many problems trying to construct a new date using setTimeInMillis(), that after over 3 hours I have given up trying to solve this by myself.

Rather than waste your time trying to dictate how I want to solve the problem... could someone please advise the most simplistic approach, and put up some code that will get me started? I am running this using Tomcat 4.1.12 and using the J2SDK 1.4.3_03 should it make any difference.

Much appreciated,
Jeff
 
I was unable to do a simple subtraction (Delta = Long - Long)

But that's the obvious way:

long old = Long.parseLong (str_old);
long now = Long.parseLong (str_now);
long delta = new - old;

 
but I found that I was unable to do a simple subtraction (Delta = Long - Long) - I got all kinds of errors. "

What errors are you getting... "I got all kinds of errors" isn't any help really! Can you post a copy of the String you get.

Relisys
 
Apologies for not being more specific about the errors... they were thrown up hours ago and after no luck I just moved on to another direction.

I still get errors even using the code supplied by stefanwagner. Here are the errors I get:

Code:
E:\Tomcat\work\Standalone\localhost\intranet\admin\test_jsp.java:60:  expected
long delta = new - old;
                 ^
E:\Tomcat\work\Standalone\localhost\intranet\admin\test_jsp.java:60: '(' or '[' expected
long delta = new - old;
                      ^

And here is the test harness I am using to create these errors:

Code:
<%@ page language="java" import="java.util.*" %>
<%
String str_authtime = "";

Cookie[] myCookieData=request.getCookies();
if (myCookieData != null)
{
  for (int i=0; i < myCookieData.length; i++)
  {
    if (myCookieData[i].getName().equals("authtime")) str_authtime = myCookieData[i].getValue();
  }
}

Calendar cal = new GregorianCalendar();
String str_now = new Long(cal.getTimeInMillis()).toString();

long old = Long.parseLong(str_authtime);
long now = Long.parseLong(str_now);
long delta = new - old;
%>

Thanks for your patience guys.

Jeff
 
OK... fixing the typo fixed all the errors...

long delta = new - old; should be now - old.

The test harness no longer errors :) Now I'll roll it into my main web app.

Thanks for this assistance... I think that my initial problem was more to do with trying to not defining the delta as a long! It's always the simple things that seem so complex if you screw up something like that.

Jeff
 
not to mention "new" being an unacceptable variable name - reserved keyword

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top