Click here to Skip to main content
15,886,741 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainActivity extends AppCompatActivity{
   TextView textView;
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      textView = findViewById(R.id.textView);
      try {
         String CurrentDate= "07/13/2022";
         String FinalDate= "07/13/2022";
         Date date1;
         Date date2;
         SimpleDateFormat dates = new SimpleDateFormat("MM/dd/yyyy");
         date1 = dates.parse(CurrentDate);
         date2 = dates.parse(FinalDate);
         long difference = Math.abs(date1.getTime() - date2.getTime());
         long differenceDates = difference / (24 * 60 * 60 * 1000);
         String dayDifference = Long.toString(differenceDates);
         textView.setText("The difference between two dates is " + dayDifference + " days");
      } catch (Exception exception) {
         Toast.makeText(this, "Unable to find difference", Toast.LENGTH_SHORT).show();
      }
   }
}
<pre lang="Java">


What I have tried:

i cant find the difference between a positve and a negeative dates
like after 365 and ????
Posted
Updated 13-Jul-22 1:20am
Comments
Richard MacCutchan 13-Jul-22 5:37am    
The difference between your two dates is zero.
Member 10974007 13-Jul-22 5:40am    
lets say
String CurrentDate= "07/13/2022";
String FinalDate= "07/12/2022";
Richard MacCutchan 13-Jul-22 5:42am    
You can say anything you like, but that does not explain what your problem is. I just tried that with those two date values and the result is:
java atest
Difference 1 day
Patrice T 13-Jul-22 5:52am    
Use Improve question to update your question.
So that everyone can pay attention to this information.
GKP1992 13-Jul-22 7:21am    
If current date is bigger than final date then difference is negative otherwise positive? Is that what you require?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900