scala - Random date between 2 given dates

Scala - Random date between 2 given dates

You can generate a random date between two given dates in Scala using the java.time.LocalDate class along with the scala.util.Random class. Here's how you can do it:

import java.time.LocalDate import scala.util.Random object RandomDate { def main(args: Array[String]): Unit = { val startDate = LocalDate.of(2020, 1, 1) val endDate = LocalDate.of(2021, 12, 31) val randomDate = randomDateBetween(startDate, endDate) println("Random Date: " + randomDate) } def randomDateBetween(startDate: LocalDate, endDate: LocalDate): LocalDate = { val startEpochDay = startDate.toEpochDay val endEpochDay = endDate.toEpochDay val randomEpochDay = startEpochDay + Random.between(0, endEpochDay - startEpochDay + 1) LocalDate.ofEpochDay(randomEpochDay) } } 

Output:

Random Date: 2021-08-22 

In this code:

  • We define the start date and end date using the LocalDate.of method from the java.time.LocalDate class.
  • We define a randomDateBetween function that takes the start date and end date as input parameters.
  • Inside the randomDateBetween function, we convert the start date and end date to epoch days using the toEpochDay method. The epoch day is a continuous count of days where day 0 is 1970-01-01.
  • We generate a random epoch day between the start and end epoch days using scala.util.Random.between.
  • We convert the random epoch day back to a LocalDate using the LocalDate.ofEpochDay method.
  • Finally, we print the random date.

Examples

  1. "Scala generate random date between two dates"

    • Description: Users often want to generate a random date between two given dates in Scala for scenarios like data generation, testing, or simulations.
    • Code:
      import java.time.LocalDate import scala.util.Random // Define start and end dates val startDate = LocalDate.of(2022, 1, 1) val endDate = LocalDate.of(2022, 12, 31) // Generate a random date between the start and end dates val randomDate = startDate.plusDays(Random.between(0, endDate.toEpochDay - startDate.toEpochDay + 1)) 
  2. "Scala random date between two dates example"

    • Description: Users might search for examples demonstrating how to generate a random date between two given dates in Scala.
    • Code:
      import java.time.LocalDate import scala.util.Random // Define start and end dates val startDate = LocalDate.of(2022, 1, 1) val endDate = LocalDate.of(2022, 12, 31) // Generate a random date between the start and end dates val randomDate = startDate.plusDays(Random.between(0, endDate.toEpochDay - startDate.toEpochDay + 1)) 
  3. "Scala random date between two dates with time"

    • Description: Users may inquire about generating a random date and time within a specified range in Scala.
    • Code:
      import java.time.{LocalDateTime, ZoneId} import scala.util.Random // Define start and end dates val startDate = LocalDateTime.of(2022, 1, 1, 0, 0) val endDate = LocalDateTime.of(2022, 12, 31, 23, 59) // Generate a random date and time between the start and end dates val randomDateTime = startDate.plusSeconds(Random.between(0, (endDate.toEpochSecond - startDate.toEpochSecond + 1).toInt)) 
  4. "Scala generate random date between two dates inclusive"

    • Description: Users might seek methods to generate a random date between two given dates inclusively in Scala, meaning both the start and end dates are possible outcomes.
    • Code:
      import java.time.LocalDate import scala.util.Random // Define start and end dates val startDate = LocalDate.of(2022, 1, 1) val endDate = LocalDate.of(2022, 12, 31) // Generate a random date between the start and end dates (inclusive) val randomDate = startDate.plusDays(Random.between(0, endDate.toEpochDay - startDate.toEpochDay + 1)) 
  5. "Scala random date between two dates without weekends"

    • Description: This query indicates users' interest in generating a random date between two given dates in Scala while excluding weekends (Saturdays and Sundays).
    • Code:
      import java.time.LocalDate import scala.util.Random // Define start and end dates val startDate = LocalDate.of(2022, 1, 1) val endDate = LocalDate.of(2022, 12, 31) // Generate a random date between the start and end dates, excluding weekends val daysBetween = endDate.toEpochDay - startDate.toEpochDay val randomDate = startDate.plusDays(Random.between(0, daysBetween)) .filterNot(date => date.getDayOfWeek.getValue >= 6) // Exclude Saturdays and Sundays 
  6. "Scala random date between two dates without duplicates"

    • Description: Users might want to generate unique random dates between two given dates in Scala, ensuring that no duplicates occur.
    • Code:
      import java.time.LocalDate import scala.util.Random // Define start and end dates val startDate = LocalDate.of(2022, 1, 1) val endDate = LocalDate.of(2022, 12, 31) // Generate a list of unique random dates between the start and end dates val daysBetween = endDate.toEpochDay - startDate.toEpochDay val randomDates = LazyList.continually(startDate.plusDays(Random.between(0, daysBetween))) .filterNot(date => date.isAfter(endDate)) .distinct 
  7. "Scala generate random date between two dates without leap years"

    • Description: Users may search for methods to generate random dates between two given dates in Scala while excluding leap years.
    • Code:
      import java.time.LocalDate import scala.util.Random // Define start and end dates val startDate = LocalDate.of(2022, 1, 1) val endDate = LocalDate.of(2022, 12, 31) // Generate a random date between the start and end dates, excluding leap years val daysBetween = endDate.toEpochDay - startDate.toEpochDay val randomDate = startDate.plusDays(Random.between(0, daysBetween)) .filterNot(date => date.isLeapYear) 
  8. "Scala random date between two dates with specific format"

    • Description: Users might want to generate a random date between two given dates in Scala and format it according to a specific date format.
    • Code:
      import java.time.LocalDate import java.time.format.DateTimeFormatter import scala.util.Random // Define start and end dates val startDate = LocalDate.of(2022, 1, 1) val endDate = LocalDate.of(2022, 12, 31) // Define date format val dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd") // Generate a random date between the start and end dates and format it val randomDate = startDate.plusDays(Random.between(0, endDate.toEpochDay - startDate.toEpochDay + 1)) .format(dateFormat) 

More Tags

nginx-config build-definition postal-code amazon-rds intellij-plugin intl-tel-input springfox google-reseller-api tobjectlist isnullorempty

More Programming Questions

More Dog Calculators

More Investment Calculators

More General chemistry Calculators

More Cat Calculators