How do I get dates between two dates?
March 28, 2011 ・0comments ・Topic: Java
In the following example code I am going to get dates between two dates. First, I need to convert the date value into milliseconds and increase startup date value from one date.
One date in milliseconds equlas to 24 * 60 * 60 * 1000L
import java.util.*;
public class DateUtil {
static final long ONE_DAY = 24 * 60 * 60 * 1000L;
public static void main(String[] args) {
getDatesBetween("03/23/2011","03/28/2011");
}
public static void getDatesBetween(String startDate,String endDate) {
long from=Date.parse(startDate); // long value of from date
long to=Date.parse(endDate); // long value of to date
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);
}
}
Post a Comment