Home java How to Compare Dates?

How to Compare Dates?

Author

Date

Category

I looked everywhere, write about before>after () , after () , compareto () . But the problem is that I have two buttons and one calendar only.

Here are the clicks of two buttons:

Public Void InitbuttonListeners (Final Date Today, Final Calendar NextYear) {
  FindViewByid (ID.LinearCalendar) .setonClicklistener (new view.onclicklistener () {
    @Override
    Public Void OnClick (View V) {
      showcalendarindialog ("Select the date of departure", R.Layout.calendar_Dialog_customized);
      if (departdata == null) {
        DialogView.init (Today, NextYear.getTime ())
            .withse selectDate (new date ());
      }
      ELSE {
        DialogView.init (Today, NextYear.getTime ())
            .withse selectDate (New Date (DepartData));
      }
      isfrom = True;
    }
  });
  FindViewByid (ID.LineAroArrive) .SetonClicklistener (new view.onclicklistener () {
    @Override
    Public Void OnClick (View V) {
      showcalendarindialog ("Select the arrival date", R.Layout.calendar_Dialog_customized);
      if (arrivedata == null) {
        DialogView.init (Today, NextYear.getTime ())
            .withse selectDate (new date ());
      } else {
        DialogView.init (Today, NextYear.getTime ())
            .withse selectDate (New Date (Arrivedata));
      }
      isfrom = false;
    }
  });
}

Click on the date:

dialogview.setondateselectedlistener (new calendarpickerview.ondateselectedlistener () {
    DateFormat DateFormat = New SimpleDateFormat ("DD Mmm yyyy");
    DateFormat DayOfweek = New SimpleDateFormat ("Eeee");
    @Override
    Public Void OndateSelected (Date Date) {
      If (ISFROM) {
        DepartData = DateFormat.Format (Date);
        Departdow = Dayofweek.format (Date);
        tvdepart.settext (departdata);
        tvsubdepart.settext (departdow);
      } else {
        Arrivedata = DateFormat.format (Date);
        arrivedow = dayofweek.format (date);
        tvarrive.settext (Arrivedata);
        tvsubarrive.settext (arrivedow);
      }
      thedialog.dismiss ();
    }
  });

I need to withdraw the toast if you have chosen the date of arrival that is less than the date of departure.
And I tried to compare Departdate with arrivedate … but an error, because they are String Type. And besides it there is nothing to compare.

Question:

How to be in this case how to compare?


Answer 1, Authority 100%

Convert DepartDate and ArriveDate Type String Type date and then compare.

Example of conversion:

string departdate = "Sat Jan 03 19:47:23 MSK 1984";
DateFormat Format = New SimpleDateFormat ("Eee Mmm DD HH: MM: SS ZZZ YYYY", Locale.English);
Date Date = format.parse (DepartDate);

Answer 2, Authority 80%

Dates can be compared with GetTime () . For example:

........................ .......
  Date Date1 = New Date (Arg1);
  Date Date2 = New Date (Arg2);

Actually, an example of comparison:

boolean b;
if (date1.gettime () & gt; date2.gettime ())
 b = true;
 ELSE B = false;
System.out.printLN (B);

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions