Java Program to Generate Date Between Given Range

This is the java program to generate all dates between the given range of dates.

Here is the source code of the Java Program to Generate Date Between Given Range. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. //This is sample program to print all the dates between range of start and end dates
  2. import java.util.Date;
  3.  
  4. public class Generate_Date 
  5. {
  6.     public static java.util.LinkedList searchBetweenDates(java.util.Date startDate, java.util.Date endDate) 
  7.     {
  8.         java.util.Date begin = new Date(startDate.getTime());
  9.         java.util.LinkedList list = new java.util.LinkedList();
  10.         list.add(new Date(begin.getTime()));
  11.  
  12.         while(begin.compareTo(endDate)<0)
  13.         {
  14.             begin = new Date(begin.getTime() + 86400000);
  15.             list.add(new Date(begin.getTime()));
  16.         }
  17.  
  18.         return list;
  19.     }
  20.  
  21.     public static void main(String[] args) throws Exception   
  22.     {   
  23.         java.util.Scanner input = new java.util.Scanner(System.in);
  24.         System.out.println("Enter the Start Date: dd/mm/yyyy");
  25.         String begin = new String();
  26.         begin = input.nextLine();
  27.  
  28.         System.out.println("Enter the End Date: dd/mm/yyyy");
  29.         String end = new String();
  30.         end = input.nextLine();
  31.  
  32.         java.util.LinkedList hitList = searchBetweenDates(
  33.         	    new java.text.SimpleDateFormat("dd/MM/yyyy").parse(begin),
  34.         	    new java.text.SimpleDateFormat("dd/MM/yyyy").parse(end));
  35.  
  36.         String[] comboDates = new String[hitList.size()];
  37.         for(int i=0; i<hitList.size(); i++)
  38.             comboDates[i] = new java.text.SimpleDateFormat("dd/MM/yyyy - E").format(((java.util.Date)hitList.get(i)));
  39.  
  40.         for(int i=0; i<comboDates.length; i++)
  41.             System.out.println(comboDates[i]);
  42.  
  43.         input.close();
  44.     }
  45. }

Output:

$ javac Generate_Date.java
$ java Generate_Date
Enter the Start Date: dd/mm/yyyy
12/8/1992
Enter the End Date: dd/mm/yyyy
12/10/1992
12/08/1992 - Wed
13/08/1992 - Thu
14/08/1992 - Fri
15/08/1992 - Sat
16/08/1992 - Sun
17/08/1992 - Mon
18/08/1992 - Tue
19/08/1992 - Wed
20/08/1992 - Thu
21/08/1992 - Fri
22/08/1992 - Sat
23/08/1992 - Sun
24/08/1992 - Mon
25/08/1992 - Tue
26/08/1992 - Wed
27/08/1992 - Thu
28/08/1992 - Fri
29/08/1992 - Sat
30/08/1992 - Sun
31/08/1992 - Mon
01/09/1992 - Tue
02/09/1992 - Wed
03/09/1992 - Thu
04/09/1992 - Fri
05/09/1992 - Sat
06/09/1992 - Sun
07/09/1992 - Mon
08/09/1992 - Tue
09/09/1992 - Wed
10/09/1992 - Thu
11/09/1992 - Fri
12/09/1992 - Sat
13/09/1992 - Sun
14/09/1992 - Mon
15/09/1992 - Tue
16/09/1992 - Wed
17/09/1992 - Thu
18/09/1992 - Fri
19/09/1992 - Sat
20/09/1992 - Sun
21/09/1992 - Mon
22/09/1992 - Tue
23/09/1992 - Wed
24/09/1992 - Thu
25/09/1992 - Fri
26/09/1992 - Sat
27/09/1992 - Sun
28/09/1992 - Mon
29/09/1992 - Tue
30/09/1992 - Wed
01/10/1992 - Thu
02/10/1992 - Fri
03/10/1992 - Sat
04/10/1992 - Sun
05/10/1992 - Mon
06/10/1992 - Tue
07/10/1992 - Wed
08/10/1992 - Thu
09/10/1992 - Fri
10/10/1992 - Sat
11/10/1992 - Sun
12/10/1992 - Mon

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.