Meaningless Drivel is fun!
The moose likes Java in General and the fly likes comparing dates in jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "comparing dates in jsp" Watch "comparing dates in jsp" New topic

comparing dates in jsp

peter moss

Joined: Aug 22, 2003
Posts: 9
Hello, I am trying to compare the LAST MODIFIED dates of some folders my application is viewing, and I only want to display the folders that were last modified TODAY. Here is what I have so far
<%@ page import="*" %>
<%@ page import="java.util.*" %>
File dir = new File("/DATA/tomcat4/webapps/tf/DICOM");
//the DICOM folder has a bunch of folders in it
String[] files = dir.list();

for (int i = 0; i < files.length; i++) {
String thepath = dir.getPath();
thepath = thepath + "/";
thepath = thepath + files[i];
File f = new File(thepath);
String newpath = f.getPath();
out.print("<BR>" + newpath + "<BR>");
long lastmod = f.lastModified();
out.print("Epoch time last modified: " + lastmod);
java.util.Date d = new java.util.Date(lastmod);
out.print("<BR>Date last modified: " + d + "<BR>");
thepath = "";
it ouputs something like this:
Epoch time last modified: 1063043798000
Mon Sep 08 11:56:38 MDT 2003
Epoch time last modified: 1063121523000
Tue Sep 09 09:32:03 MDT 2003
Epoch time last modified: 1062774534000
Fri Sep 05 09:08:54 MDT 2003
But I really only want to show the one in the middle that was last modified today.
Any ideas?
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63844

Hi peter,
Even though you are performing this function in a JSP, it's still a general Java question, so I'm moving this to Java in General/Intermediate.

[Asking smart questions] [About Bear] [Books by Bear]
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
One way to get a "Date" instance that has today's Date but no time (i.e., midnight) is:
Calendar todayCal = Calendar.getInstance();
Date todayDate = todayCal.getTime();
System.out.println("TODAY: " + todayDate);
You can then do a Date compare:
java.util.Date d = new java.util.Date(lastmod);
if (d.after(todayDate)) {
... // print the values you want
Also, in your code you are using:
String[] files = dir.list();
and then in the loop you are manually building the file path. There is another method you could use to get an array of File instances, rather than just the names:
File[] files = dir.listFiles();
Inside your loop you can use "files[i]" as the File instance and can save all the effort of concatenating the directory and creating the File.
[ September 09, 2003: Message edited by: Wayne L Johnson ]
I agree. Here's the link:
subject: comparing dates in jsp
jQuery in Action, 3rd edition