I'm trying to generate a random date of birth for people in my database using a Java program. How would I do this?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
I think this will do the trick:
Generating random Date of Births:
You can checkout randomizer for random data generation.This library helps to create random data from given Model class.Checkout below example code.
As there are many built in data generator is accessible using annotation,You also can build custom data generator.I suggest you to go through documentation provided on library page.
You need to define a random date, right?
A simple way of doing that is to generate a new
Date
object, using along
(time in milliseconds since 1st January, 1970) and substract a randomlong
:(RandomUtils is taken from Apache Commons Lang).
Of course, this is far to be a real random date (for example you will not get date before 1970), but I think it will be enough for your needs.
Otherwise, you can create your own date by using
Calendar
class:For Java8 -> Assumming the data of birth must be before current day: