Very simple Java problem, please help

P

phillip.s.powell

My Java-based chatroom is failing to do its automatic CRON job to
delete latent messages and nicknames that linger on the server more
than an hour.

I set up a few JSP scripts that will retrieve and remove these files by
comparing the last modified date of messages.txt to determine if the
very last message left is more than an hour old; if so, delete it.

As of 1/1/2005, this process no longer works and I am baffled as to why
this is happening; I am getting comparison integer values from "then"
to "now" of 1 microsecond every time, no matter how far apart the dates
of "now" and "then".

Meanwhile, in the attempt to debug this problem I am getting nasty
compiler errors.

What I'm doing very simply is to try to get the last modified date of a
file, period.

Here is the code:

try {
File file = new File(request.getParameter("fileName"));
Long blah = file.lastModified();
Date sometime = new Date(blah);
out.print("last modified: " + sometime + "\n\n<P><P>");
} catch (Exception e) {
e.printStackTrace();
}

Here is the error:

Note: sun.tools.javac.Main has been deprecated.
/~ppowell/includes/file_retrieval.jsp:11: Incompatible type for
declaration.
Can't convert long to java.lang.Long.
java.lang.Long blah = file.lastModified();
^
/~ppowell/includes/file_retrieval.jsp:12: Incompatible type for
constructor.
Can't convert java.lang.Long to java.lang.String.
Date sometime = new Date(blah);
^
2 errors, 1 warning

I was under the impression that file.lastModified() returns a Long.
Can someone debug this very simple problem for me and tell me what I
did wrong? That way I can make a bit more progress in the attempt to
debug this:

package ppowell;

import java.io.*;
import java.util.Vector;

/**
* Retrieves flat files for parse/usage
*
* @version JDK 1.4
* @author Phil Powell
* @package PPOWELL
*/
public class FlatFileRetriever implements Retriever {

private String fileName = "";

/**
* Constructor
*
* @access public
* @param String fileName
*/
public FlatFileRetriever(String fileName) {
this.fileName = fileName;
}

//------------------- --* GETTER/SETTER METHODS *--
---------------------

/**
* Must contain this method as it implements Retriever but leave blank
to be
* overriden later
*
* @access public
* @return String
*/
public String getCookieVal() { // STRING METHOD
return "";
}

/**
* Retrieve file contents if dynamic via synchronized block
*
* @access public
* @return Vector contentVector
*/
public synchronized Vector getDynamicFileContents() { // VECTOR METHOD
Vector contentVector = getFileContents();
return contentVector;
}

/**
* Retrieve file contents into Vector
*
* @access public
* @return Vector contentVector
*/
public Vector getFileContents() { // VECTOR METHOD
Vector contentVector = new Vector();
try {
BufferedReader in = new BufferedReader(new
FileReader(this.fileName));
String stuff = "";
while ((stuff = in.readLine()) != null) contentVector.add(stuff);
in.close();
} catch (Exception e) {
e.printStackTrace();
}
return contentVector;
}


/**
* Must contain this method as it implements Retriever interface but
leave blank to
* be overridden later
*
* @access public
* @return String HTML
*/
public String getHTML() { // STRING METHOD
return "";
}


/**
* Get last modified date of file referenced by string
*
* @access public
* @return long lastModified date
*/
public long getLastModified() { // LONG METHOD
File file = new File(this.fileName);
if (file.exists()) return file.lastModified();
return 0;
}
//------------------ --* END OF GETTER/SETTER METHODS *--
-------------------

/**
* Return boolean if file exists or not
*
* @access public
* @return boolean exists
*/
public boolean exists() { // BOOLEAN METHOD
File file = new File(this.fileName);
return file.exists();
}

}


Thanx
Phil
 
V

VisionSet

Here is the error:

Note: sun.tools.javac.Main has been deprecated.
/~ppowell/includes/file_retrieval.jsp:11: Incompatible type for
declaration.
Can't convert long to java.lang.Long.
java.lang.Long blah = file.lastModified();
^
/~ppowell/includes/file_retrieval.jsp:12: Incompatible type for
constructor.
Can't convert java.lang.Long to java.lang.String.
Date sometime = new Date(blah);
^
2 errors, 1 warning

I was under the impression that file.lastModified() returns a Long.
Can someone debug this very simple problem for me and tell me what I
did wrong? That way I can make a bit more progress in the attempt to
debug this:

It returns the primitive long not the object Long.
I suspect all you need to do for that part of the code is just declare blah
as the long rather than Long, then your Date constructor will have the right
type of its argument.
 
P

phillip.s.powell

Thanx, wow, what a simple solution to a stupid question..

Now I am able to view the last modified date, using this code:
Date now = new Date();
try {
FlatFileRetriever retriever = new
FlatFileRetriever(request.getParameter("fileName"));
if (retriever.exists()) { // ONLY PERFORM FIRST IF FILE EXISTS IN
THE FIRST PLACE
Date then = new Date(retriever.getLastModified());
out.print("now: " + now + "\n\n<P>");
out.print("last modified using retriever: " + then + "\n\n<P>");
out.print(now.compareTo(then));
} catch (Exception e) {
// DO STUFF
}

Here is a typical result:

now: Sun Jan 16 14:28:24 PST 2005
last modified using retriever: Sun Jan 16 02:18:49 PST 2005

The difference between these two dates is far greater than 3000 (50
minutes), however, here is the result every time:
1

using: now.compareTo(then)

What is going on??

Phil
 
A

Andrew McDonagh

Thanx, wow, what a simple solution to a stupid question..

Now I am able to view the last modified date, using this code:
Date now = new Date();
try {
FlatFileRetriever retriever = new
FlatFileRetriever(request.getParameter("fileName"));
if (retriever.exists()) { // ONLY PERFORM FIRST IF FILE EXISTS IN
THE FIRST PLACE
Date then = new Date(retriever.getLastModified());
out.print("now: " + now + "\n\n<P>");
out.print("last modified using retriever: " + then + "\n\n<P>");
out.print(now.compareTo(then));
} catch (Exception e) {
// DO STUFF
}

Here is a typical result:

now: Sun Jan 16 14:28:24 PST 2005
last modified using retriever: Sun Jan 16 02:18:49 PST 2005

The difference between these two dates is far greater than 3000 (50
minutes), however, here is the result every time:
1

using: now.compareTo(then)

What is going on??

Phil

try reading...

http://java.sun.com/j2se/1.3/docs/api/java/util/Date.html#compareTo(java.util.Date)


Then apply that to your code...

answer = now.compareTo(Then)

answer equals is greater than 0, so 'now' is 'after' 'then'
 
P

phillip.s.powell

*sigh* that doesn't help me at all. I have to know that it is a
specific unit of time after "then", not being "in general after
'then'". "Now" must be >= 50 minutes after "then". What can help me
do this?

Phil
 
D

dar7yl

*sigh* that doesn't help me at all. I have to know that it is a
specific unit of time after "then", not being "in general after
'then'". "Now" must be >= 50 minutes after "then". What can help me
do this?

<code fragment>
Date now = new (Date);
Date then = // date of object
long diff = now.getTime() - then.getTime();
System.out.println("The difference is " + diff / MS_PER_MIN + "
minutes");
</code>
 
A

Andrew McDonagh

*sigh* that doesn't help me at all. I have to know that it is a
specific unit of time after "then", not being "in general after
'then'". "Now" must be >= 50 minutes after "then". What can help me
do this?

Phil

*SIGH* no do your own work... I gave you the url of the Date class
which specifies all of its capabilities. Just read and learn, the answer
is there.
 
A

Andrew McDonagh

dar7yl said:
<code fragment>
Date now = new (Date);
Date then = // date of object
long diff = now.getTime() - then.getTime();
System.out.println("The difference is " + diff / MS_PER_MIN + "
minutes");
</code>

Why are doing this guys homework for him, he's got to learn to read
about stuff, rather than simply copy & paste other peoples code.
 
D

dar7yl

The homework assignment today, kiddies, is to find the error I :)accidently
on purpose:) left in the code.
regards,
Dar7yl
 
A

Andrew McDonagh

:)

Very good, I must admit I didn't even look at the code you posted, just
that you did post something
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top