Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

comparing dates in jsp

 
peter moss
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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="java.io.*" %>
<%@ 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:
/DATA/tomcat4/webapps/tf/DICOM/1689236
Epoch time last modified: 1063043798000
Mon Sep 08 11:56:38 MDT 2003
/DATA/tomcat4/webapps/tf/DICOM/1662170
Epoch time last modified: 1063121523000
Tue Sep 09 09:32:03 MDT 2003
/DATA/tomcat4/webapps/tf/DICOM/1111111
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
Marshal
Pie
Posts: 64833
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
bear
 
Wayne L Johnson
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way to get a "Date" instance that has today's Date but no time (i.e., midnight) is:
Calendar todayCal = Calendar.getInstance();
todayCal.clear(Calendar.HOUR_OF_DAY);
todayCal.clear(Calendar.HOUR);
todayCal.clear(Calendar.MINUTE);
todayCal.clear(Calendar.SECOND);
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic