How can we compare dates in java?

0 votes
Is it possible to compare dates in java? Any help?
Apr 27, 2018 in Java by Akrati
• 3,150 points

4 answers to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

You can refer the below code for comparing dates in java:

if(!historyDate.after(todayDate) && !futureDate.before(todayDate)) 
    /* historyDate <= todayDate <= futureDate */ 
answered Apr 27, 2018 by Daisy
• 8,040 points
0 votes

You can use compareTo():


answered Aug 1, 2018 by parth295
• 4,600 points
0 votes
Date today = new Date();                   
  Date myDate = new Date(today.getYear(),today.getMonth()-1,today.getDay());
  System.out.println("My Date is"+myDate);    
  System.out.println("Today Date is"+today);
  if (today.compareTo(myDate)<0)
      System.out.println("Today Date is Lesser than my Date");
  else if (today.compareTo(myDate)>0)
      System.out.println("Today Date is Greater than my date"); 
      System.out.println("Both Dates are equal"); 
answered Sep 4, 2018 by Daisy
• 8,040 points
0 votes
public static String daysBetween(String day1, String day2) {
    String daysBetween = "";
    SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    try {
        Date d1 = myFormat.parse(day1);
        Date d2 = myFormat.parse(day2);
        long diff = d2.getTime() - d1.getTime();
        daysBetween = ""+(TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS));
    } catch (ParseException e) {
    return daysBetween;
answered Sep 5, 2018 by Sushmita
• 6,840 points

Related Questions In Java

0 votes
1 answer

How can we get file extension in Java?

To get the file extension, we can ...READ MORE

answered May 8, 2018 in Java by parth295
• 4,600 points
0 votes
2 answers

How can we remove an element from an array in Java?

You can use ArrayUtils class remove method which ...READ MORE

answered May 24, 2018 in Java by UshaK
0 votes
1 answer

How can we resolve ClassNotFoundException in Java?

Your classpath is broken. Depending on how you ...READ MORE

answered May 22, 2018 in Java by Akrati
• 3,150 points
0 votes
1 answer

How can we add leading zeros to the number in Java?

To add zeros to a number, we ...READ MORE

answered May 25, 2018 in Java by Akrati
• 3,150 points
0 votes
1 answer

How can one day be added to a date?

One possible solution could be using calendar ...READ MORE

answered Jun 8, 2018 in Java by Daisy
• 8,040 points
0 votes
1 answer

How can I calculate number of days between two dates?

You may refer this. This might work ...READ MORE

answered Jul 19, 2018 in Java by Akrati
• 3,150 points
0 votes
1 answer

How to calculate the interval between 2 dates

The following code might be helpful: public static ...READ MORE

answered Aug 29, 2018 in Java by bug_seeker
• 15,300 points
+5 votes
3 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,380 points

edited Nov 6, 2018 by Omkar 5,519 views
0 votes
1 answer

How can we compare objects by multiple fields in Java?

You can implement a Comparator which compares two Person objects, and ...READ MORE

answered Jan 9 in Java by Daisy
• 8,040 points
0 votes
4 answers

How can we define global variables in java?

class G { static String globalVar ...READ MORE

answered Jan 29 in Java by zemiak

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.