Java Year Get getRandomDate(int firstYar, int lastYear)

Here you can find the source of getRandomDate(int firstYar, int lastYear)

Description

get Random Date

License

Open Source License

Declaration

public static GregorianCalendar getRandomDate(int firstYar, int lastYear) 

Method Source Code

//package com.java2s;
/*/*from  www  .  j av a 2s.co m*/
 * *********************************************************************
 * Copyright (c) 2010 Pedro Gomes and Universidade do Minho.
 * All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * ********************************************************************
 */

import java.util.*;

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

    public static GregorianCalendar getRandomDate(int firstYar, int lastYear) {
        int month, day, year, maxday;

        year = getRandomInt(firstYar, lastYear);
        month = getRandomInt(0, 11);

        maxday = 31;
        if (month == 3 | month == 5 | month == 8 | month == 10) {
            maxday = 30;
        } else if (month == 1) {
            maxday = 28;
        }

        day = getRandomInt(1, maxday);
        return new GregorianCalendar(year, month, day);
    }

    public static int getRandomInt(int lower, int upper) {

        int num = (int) Math.floor(rand.nextDouble() * ((upper + 1) - lower));
        if (num + lower > upper || num + lower < lower) {
            System.out.println("ERROR: Random returned value of of range!");
            System.exit(1);
        }
        return num + lower;
    }
}

Related

  1. getLastDayOfYear(Date date)
  2. getNextYear()
  3. getNextYearFirst()
  4. getNextYearMonthStr()
  5. getOfflimitDates(int year)
  6. getSpecficYearEnd(Date date, int amount)
  7. getStartEndOFWeek(int enterWeek, int enterYear)
  8. getStartOfYear()
  9. getThisYear()