Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Random;

public class Main {
    private static Random rand = new Random();

    public static Calendar getRandomDate(int baseYear) {
        synchronized (rand) {
            Calendar today = Calendar.getInstance();
            int year = rand.nextInt(today.get(Calendar.YEAR) - baseYear + 1) + baseYear;
            int month = year == today.get(Calendar.YEAR) ? rand.nextInt(today.get(Calendar.MONTH) - 1) + 1
                    : rand.nextInt(12) + 1;
            int day = year == today.get(Calendar.YEAR) ? rand.nextInt(today.get(Calendar.DAY_OF_WEEK)) + 1
                    : rand.nextInt(29) + 1;

            return new GregorianCalendar(year, month, day);
        }
    }
}