How to get all dates between two dates in Android.
For example. I have two Strings.
String first="2012-07-15";
String second="2012-07-21";
I convert and get dates from these strings.
DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
DateFormat df2 = new SimpleDateFormat("MMM dd");
String mydate = df2.format(df1.parse(first));
This way I get both dates from first and second String.
Now I also display all dates between these two dates.
We can get the dates between two dates with single method call using the dedicated datesUntil method of a LocalDate class. The datesUntill returns the sequentially ordered Stream of dates starting from the date object whose method is called to the date given as method argument.
The idea is quite simple, just use Calendar class to roll the month back and forward to create a “date range”, and use the Date. before() and Date. after() to check if the Date is within the range.
it is better not to use any hardcoded values in date calculations. we can rely on java Calendar
class methods to do this task
see the code
private static List<Date> getDates(String dateString1, String dateString2)
{
ArrayList<Date> dates = new ArrayList<Date>();
DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = null;
Date date2 = null;
try {
date1 = df1 .parse(dateString1);
date2 = df1 .parse(dateString2);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cal1 = Calendar.getInstance();
cal1.setTime(date1);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(date2);
while(!cal1.after(cal2))
{
dates.add(cal1.getTime());
cal1.add(Calendar.DATE, 1);
}
return dates;
}
and use it as below
List<Date> dates = getDates("2012-02-01", "2012-03-01");
for(Date date:dates)
System.out.println(date);
public class DummyWorks extends Activity
{
static final long ONE_DAY = 24 * 60 * 60 * 1000L;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getDatesBetween("03/23/2011","03/28/2011");
}
public static void getDatesBetween(String startDate,String endDate) {
long from=Date.parse(startDate);
long to=Date.parse(endDate);
int x=0;
while(from <= to) {
x=x+1;
System.out.println ("Dates : "+new Date(from));
from += ONE_DAY;
}
System.out.println ("No of Dates :"+ x);
}
}
Solution in Kotlin: It will return List of Date between 2 dates.
fun getDatesBetween(dateString1:String, dateString2:String):List<String> {
val dates = ArrayList<String>()
val input = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
var date1:Date? = null
var date2:Date? = null
try
{
date1 = input.parse(dateString1)
date2 = input.parse(dateString2)
}
catch (e:ParseException) {
e.printStackTrace()
}
val cal1 = Calendar.getInstance()
cal1.time = date1
val cal2 = Calendar.getInstance()
cal2.time = date2
while (!cal1.after(cal2))
{
val output = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
dates.add(output.format(cal1.time))
cal1.add(Calendar.DATE, 1)
}
return dates
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With