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.Random;

public class Main {
    public static Calendar getRandomApodDate() {
        Calendar newCalendar = Calendar.getInstance();

        Calendar today = Calendar.getInstance();
        int currentYear = today.get(Calendar.YEAR);
        int currentMonth = today.get(Calendar.MONTH);
        int currentDay = today.get(Calendar.DATE);

        int firstApodYear = 1995;
        int firstApodMonth = 5;
        int firstApodDay = 20;

        Random rnd = new Random();
        int newYear = rnd.nextInt(currentYear - firstApodYear + 1) + firstApodYear;

        int minMonth = 0;
        int maxMonth = 11;
        if (newYear == firstApodYear)
            minMonth = firstApodMonth;
        if (newYear == currentYear)
            maxMonth = currentMonth;
        int newMonth = rnd.nextInt(maxMonth - minMonth + 1) + minMonth;

        newCalendar.set(newYear, newMonth, 1);

        int minDay = 1;
        int maxDay = newCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        if (newYear == firstApodYear && newMonth == firstApodMonth)
            minDay = firstApodDay;
        if (newYear == currentYear && newMonth == currentMonth)
            maxDay = currentDay;
        int newDay = rnd.nextInt(maxDay - minDay + 1) + minDay;

        newCalendar.set(newYear, newMonth, newDay);
        return newCalendar;
    }
}