Wednesday, 3 August 2016

Generate Random Date in between Start Date and End Date(Java)


Generating random Date using java for Selenium WebDriver by taking inputs as format of date,Start date and End Date.
Please find the below Reusable method for the same

Sample Code for generating Random Date in between Start Date and End Date(Java)::

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class GenerateRandomDate {


 
public static String generateRandomDate(String Format,String startDate,String endDate) throws ParseException
{
DateFormat formatter = new SimpleDateFormat(Format);
Calendar cal=Calendar.getInstance();
cal.setTime(formatter.parse(startDate));
Long value1 = cal.getTimeInMillis();

cal.setTime(formatter.parse(endDate));
Long value2 = cal.getTimeInMillis();

long value3 = (long)(value1 + Math.random()*(value2 - value1));
cal.setTimeInMillis(value3);
return formatter.format(cal.getTime());
     }

 
 
    public static void main(String args[]) throws ParseException{
   
    System.out.println(GenerateRandomDate.generateRandomDate("dd MMM yyyy", "01 Aug 2016", "01 Sep 2017"));
   
   
    }
 
 
 
 

}

9 comments:

  1. 66A98DF0FCChristian778D65879827 November 2024 at 16:56

    AC4985AFDB
    şov
    cialis
    steroid satın al

    ReplyDelete