package com.jjbanks.learning.util;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Random;
import java.util.logging.Logger;

/**
 * 
 * Utility for management of date-time operations.
 * Provided random generation capabilities for use by applications
 * needing to generate random dates and times.
 * 
 * @author jjbanks.com
 *
 */
public class DateTimeUtil {

	private static Logger logger = Logger.getLogger(DateTimeUtil.class.getName());

	/**
	 * 
	 * @return a randomly generated date-time
	 */
	public static LocalDateTime generateRandomDateTime() {

		RandomDate rd = new RandomDate(LocalDate.of(1900, 1, 1), LocalDate.of(2010, 1, 1));
		logger.info("RandomDate: " + rd);

		LocalDate ld = rd.nextDate();
		logger.info("LocalDate: " + ld);

		RandomTime rt = new RandomTime();
		LocalTime lt = rt.nextTime();
		logger.info("LocalTime: " + lt);

		LocalDateTime ldt = LocalDateTime.of(ld, lt);
		logger.info("LocalDateTime: " + ldt);

		return ldt;
	}

	public static LocalDateTime generateRandomDateTime(LocalDate from, LocalDate to) {

		if(!from.isBefore(to)) {
			throw new IllegalArgumentException("from local date must be before the 'to' local date");
		}
		RandomDate rd = new RandomDate(from, to);
		LocalDate ld = rd.nextDate();
		RandomTime rt = new RandomTime();
		LocalTime lt = rt.nextTime();
		LocalDateTime ldt = LocalDateTime.of(ld, lt);

		return ldt;
	}

	/**
	 * 
	 * Representation of random time
	 *
	 */
	private static class RandomTime {

		private final Random random;

		/**
		 * Constructor with random instantiation.
		 */
		public RandomTime() {
			this.random = new Random();
		}

		/**
		 *
		 * @return randomized time
		 */
		public LocalTime nextTime() {

			int randomHour = random.nextInt(23);
			int randomMinute = random.nextInt(59);
			int randomSecond = random.nextInt(59);
			int randomNanosecond = random.nextInt(999);

			return LocalTime.of(randomHour, randomMinute, randomSecond, randomNanosecond);

		}
	}

	/**
	 * Representation of random date
	 */
	private static class RandomDate {

		private final LocalDate minDate;
		private final LocalDate maxDate;
		private final Random random;

		public RandomDate(LocalDate minDate, LocalDate maxDate) {
			this.minDate = minDate;
			this.maxDate = maxDate;
			this.random = new Random();
		}

		public LocalDate nextDate() {
			int minDay = (int) minDate.toEpochDay();
			int maxDay = (int) maxDate.toEpochDay();
			long randomDay = minDay + random.nextInt(maxDay - minDay);
			return LocalDate.ofEpochDay(randomDay);
		}

		@Override
		public String toString() {
			return "RandomDate{" + "maxDate=" + maxDate + ", minDate=" + minDate + '}';
		}
	}

}

@@ -1010,16 +1010,116 @@
 dt;%0A%09%7D%0A%0A
+%09/**%0A%09 * %0A%09 * @param from%0A%09 * @param to%0A%09 * @return local date-time that is randomly generated%0A%09 */%0A
 %09public 
@@ -1319,16 +1319,19 @@
 %22);%0A%09%09%7D%0A
+%09%09%0A
 %09%09Random
@@ -1398,16 +1398,19 @@
 Date();%0A
+%09%09%0A
 %09%09Random
@@ -2297,16 +2297,122 @@
 andom;%0A%0A
+%09%09/**%0A%09%09 * Constructor using minimum and maximum date range%0A%09%09 * @param minDate%0A%09%09 * @param maxDate%0A%09%09 */%0A
 %09%09public
@@ -2553,16 +2553,87 @@
 );%0A%09%09%7D%0A%0A
+%09%09/**%0A%09%09 * Generates another local date %0A%09%09 * @return local date%0A%09%09 */%0A
 %09%09public

Comment updates