We help IT Professionals succeed at work.

compare date in java??

boyyle asked
i have some problems on how to compare the DATE.

for example,i use the below code to get the today DATE.

java.util.Date today = new java.util.Date();
SimpleDateFormat datestr = new SimpleDateFormat("yyyy-MM-dd");
String today_date = datestr.format(today);

then,i have another DATE which is stored in the database.

lets say the DATE that i stored in datebase is database_date.

so now i would like to check whether database_date is 40 days older
than today_date?????


p/s: the date format in the database(PoINTBASE) is yyyy-mm-dd
Watch Question

Chad SmithSoftware Engineer

Not quite sure about 40 days, but hopefully this code will get you on the right track, it is code I use to get a date one month ago.

Calendar currentDate = new GregorianCalendar();
int month = (currentDate.get(GregorianCalendar.MONTH)) + 1;
int day = (currentDate.getGregorianCalendar.DAY_OF_MONTH));
int year = (currentDate.get(GregorianCalendar.YEAR));
String DateMonthAgo = year + "-" + month + "-" + day;

U can use getMonth() and get Year or there is also dateDifference class that u can use to find the difference between two dates..
Mick BarryJava Developer
Top Expert 2010

Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();

int day1 = cal1.get(Calendar.DAY_OF_YEAR);
int day2 = cal2.get(Calendar.DAY_OF_YEAR);

if (d2-d1>40)
  System.out.println("More than 40 days difference");
Timestamp today_date = new Timestamp(System.currentTimeMillis());   // Current date
Timestamp database_date = null; // database date
long date_difference = 0;

String query = "SELECT * FROM Users where ID = 3"; // some query
ResultSet rs = stmt.executeQuery(query);
while (rs.next()){
  database_date = rs.getTimestamp("CreationDate"); // get date from database
  date_difference = (today_date.getTime() - database_date.getTime()); // in millisecond
  date_difference /= 1000*60*60*24; // convert it into days
  if (date_difference == 40 )
     System.out.println("40 days older...");
//Connection to the database server is closed now

Mick BarryJava Developer
Top Expert 2010

> date_difference /= 1000*60*60*24; // convert it into days

This assumes that each day has 24 hours which is not always the case.
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101, Netminder or Mindphaser will return to finalize these if they are still open in 7 days.  Experts, please post closing recommendations before that time.

Below are your open questions as of today.  Questions which have been inactive for 21 days or longer are considered to be abandoned and for those, your options are:
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> YOU CANNOT DELETE A QUESTION with comments; special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and please keep them updated. If you are a KnowledgePro user, use the Power Search option to find them.  

Questions which are LOCKED with a Proposed Answer but do not help you, should be rejected with comments added.  When you grade the question less than an A, please comment as to why.  This helps all involved, as well as others who may access this item in the future.  PLEASE DO NOT AWARD POINTS TO ME.

To view your open questions, please click the following link(s) and keep them all current with updates.

*****  E X P E R T S    P L E A S E  ******  Leave your closing recommendations.
If you are interested in the cleanup effort, please click this link
POINTS FOR EXPERTS awaiting comments are listed in the link below
Moderators will finalize this question if in @7 days Asker has not responded.  This will be moved to the PAQ (Previously Asked Questions) at zero points, deleted or awarded.
Thanks everyone.
Moderator @ Experts Exchange
Thank you for returning and finalizing your questions.
:) Moondancer - EE Moderator

Explore More ContentExplore courses, solutions, and other research materials related to this topic.