Question: Hi looking for a sample code which will return a RandomDateTime as 1968-02-03 23:02:03 over a range (1960,1970).

YYYY-MM-DD HH:MM:SS. 

my code is returning timezone in between which I don't want. 
...

Answer: 
java.time
---------

```
    Random rand = new Random();
    LocalDateTime minInclusive = LocalDateTime.of(1960, Month.JANUARY, 1, 0, 0);
    LocalDateTime maxExclusive = LocalDateTime.of(1971, Month.JANUARY, 1, 0, 0);

    int seconds = Math.toIntExact(ChronoUnit.SECONDS.between(minInclusive, maxExclusive));
    int choice = rand.nextInt(seconds);
    LocalDateTime randomDateTime = minInclusive.plusSeconds(choice);
    System.out.println(randomDateTime);

```

Example output (from running just now):

> 
> 1960-12-05T19:05:28
> 
> 
> 

I am using `LocalDateTime` because you said you wanted no time zone, but you risk getting a non-exisitng time, typically a time in the spring gap when the clocks are turned forward when sumer time (DST) begins. To avoid this risk, use `ZonedDateTime` and specify the time zone. The code will be very similar to the above.

Calling [`between`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/temporal/TemporalUnit.html#between(java.time.temporal.Temporal,java.time.temporal.Temporal)) on [`ChronoUnit.SECONDS`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/temporal/ChronoUnit.html#SECONDS) calculates elapsed time as whole seconds.

The call to [`Math.toIntExact`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Math.html#toIntExact(long)) is a safer way to go from a 64-bit `long` to an 32-bit `int`. Throws an exception if the value overflows.

The output from printing `randomDateTime` is in [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format. If you want a different format (for example, without the `T` in the middle), you may format it using a [`DateTimeFormatter`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/format/DateTimeFormatter.html). [Search for how](https://duckduckgo.com/?q=site%3Astackoverflow.com%20java%20DateTimeFormatter&t=osx&ia=web).

**Link:** [Oracle tutorial: Date Time](https://docs.oracle.com/javase/tutorial/datetime/) explaining how to use *java.time*.
You can get Randon date using ThreadLocalRandom class.
Here is quick fix for you. Please check following code.

**Input :**

Enter the Start Date: dd/mm/yyyy

01/01/1960

Enter the End Date: dd/mm/yyyy

01/01/1970

**Output :**

1969-04-18 06:30:54

```
        Scanner input = null;
        try {
            input = new Scanner(System.in);
            System.out.println("Enter the Start Date: dd/mm/yyyy");
            String begin = new String();
            begin = input.nextLine();
            SimpleDateFormat startFormat = new SimpleDateFormat("dd/mm/yyyy");
            Date startDate = startFormat.parse(begin);

            System.out.println("Enter the End Date: dd/mm/yyyy");
            String end = new String();
            end = input.nextLine();
            SimpleDateFormat endFormat = new SimpleDateFormat("dd/mm/yyyy");
            Date endDate = endFormat.parse(end);

            Date randomDate = new Date(ThreadLocalRandom.current()
                    .nextLong(startDate.getTime(), endDate.getTime()));

            String pattern = "yyyy-MM-dd hh:mm:ss";
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
            String randomDateFormat = simpleDateFormat.format(randomDate);
            System.out.println(randomDateFormat);

        }
        catch (Exception ex) {
            ex.printStackTrace();
        }finally{
            input.close();
        }

```

Hope this solution works.