Sunday, October 29, 2017

Date Comparision And Change Date Format

Date Comparision And Change Date Format

Hi ! guys Today we are going to see how to compare two date in android its very simple.........

In this post we going to see two thing one is change the date format, second one is date comparision.


Date Comparision:
Step 1
Create object for SimpleDateFormat
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");

Step 2
Create Object for Calendar using getInstance() method.
Calendar calendar = Calendat.getInstance();
calendar.setTime(calendar.getTime());
Date newDate = calendar.getTime();

Step 3
String strCompareDate = "22/10/2017";
String strCurrentDate = df.format(newDate);


Step 4

if(strCurrentDate .compareTo(strCompareDate ) < 0){
//CurrentDate less than  systemDate
}else if(strCurrentDate .compareTo(strCompareDate ) > 0){
//CurrentDate greater than  systemDate
}else{
//Both are equal
}


Example:

SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Calendar calendar = Calendat.getInstance();
calendar.setTime(calendar.getTime());
Date newDate = calendar.getTime();
String strCompareDate = "22/10/2017";
String strCurrentDate = df.format(newDate);

if(strCurrentDate .compareTo(strCompareDate ) < 0){

//CurrentDate less than  systemDate

}else if(strCurrentDate .compareTo(strCompareDate ) > 0){

//CurrentDate greater than  systemDate

}else{
//Both are equal
}

Date Format Change:
we get date from Calendar in any format , change using SimpleDateFormat
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");


No comments:

Post a Comment