• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

comparing dates in jsp

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic