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
108 views

4 answers to this question.

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,090 points
0 votes

You can use compareTo():

date1.compareTo(date2);

answered Aug 1, 2018 by Parth
• 4,630 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"); 
  else
      System.out.println("Both Dates are equal"); 
answered Sep 4, 2018 by Daisy
• 8,090 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) {
        e.printStackTrace();
    }
    return daysBetween;
}
answered Sep 5, 2018 by Sushmita
• 6,890 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 Parth
• 4,630 points
820 views
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
427 views
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
139 views
0 votes
2 answers

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

From Java 1.5 you can use the String.format method. ...READ MORE

answered Aug 26, 2019 in Java by Sirajul
• 54,020 points
1,051 views
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,090 points
60 views
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
449 views
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,390 points
74 views
+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,420 points

edited Nov 6, 2018 by Omkar 24,479 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, 2019 in Java by Daisy
• 8,090 points
189 views
0 votes
4 answers

How can we define global variables in java?

class G { static String globalVar ...READ MORE

answered Jan 29, 2019 in Java by zemiak
18,456 views