Compare Current Date With Another Date In Jsp

I am using label to get date from database., and then comparing system date with database date from label. ADI@345 4-Jan-18 1:32am String was not recognized as a valid DateTime. First you need to convert to dates to same format. You can convert entered date like this. Var enteredDate = new date('entered date'); then compare with today's date enteredDate Date Thanks, Hope it may help you. Please revert back if you have any queries.

Greenhorn
posted 17 years ago
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?
Marshal
posted 17 years ago
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

[Asking smart questions] [About Bear] [Books by Bear]

Ranch Hand
posted 17 years ago
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 ]
AnotherJsp

In the previous section, we discussed the date methods as well as the constructors.

Here, with the help of those methods, we will learn to compare dates.

Basically, there are different ways by which we can compare dates, such as:

  1. Comparing two dates with one another.
  2. Comparing date with time.
  3. Comparing dates using getTime()

Comparing two dates with one another

Example:

Test it Now

Comparing date with time

Example 1: Comparing different dates with different timings

Test it Now

Example2: Comparing same dates with disimilar timings

Test it Now

Comparing date with getTime()

A better approach to make comparison between dates is to use getTime() function. This function lets converting date into numeric value to directly compare them.

Example1: Comparing current date and time with a given date and time.

Test it Now

Compare Current Date With Another Date In Jsp Connect

Example2: Comparing two different dates with different timings.

Test it Now

Thus, we can compare dates in many possible ways.

Changing Date Format

We can also change or set the format through JavaScript code. The function getFullYear(), GetMonth(), and getDate() allows to set the format of date accordingly.

Example1: Changing the date format to 'yyyy-mm-dd'.

Compare Current Date With Another Date In Jsp Form

Test it Now

We can also set the date and time format according to our need.

Compare Current Date With Another Date In Jsp File

Example2: Changing the datetime format to 'yyyy-dd-mm hh:mm:ss'.

Compare Current Date With Another Date In Jsp Login

Test it Now