Java Random Date getRandomDate(Random ran, boolean idNewID)

Here you can find the source of getRandomDate(Random ran, boolean idNewID)

Description

get Random Date

License

Apache License

Declaration

private static String getRandomDate(Random ran, boolean idNewID) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.text.NumberFormat;

import java.util.Random;

public class Main {
    private static String getRandomDate(Random ran, boolean idNewID) {
        if (ran == null) {
            return "";
        }//from  w  ww. java  2 s .com
        int year = 0;
        if (idNewID) {
            year = 1900 + ran.nextInt(2007 - 1900);
        } else {
            year = 1 + ran.nextInt(99);
        }
        int month = 1 + ran.nextInt(12);
        int day = 0;
        if (month == 2) {
            day = 1 + ran.nextInt(28);
        } else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10
                || month == 12) {
            day = 1 + ran.nextInt(31);
        } else {
            day = 1 + ran.nextInt(30);
        }
        NumberFormat nf = NumberFormat.getIntegerInstance();
        nf.setMaximumIntegerDigits(2);
        nf.setMinimumIntegerDigits(2);
        return Integer.toString(year) + nf.format(month) + nf.format(day);
    }
}

Related

  1. getDateRand()
  2. getDateRand(Date date)
  3. getDateRandom()
  4. getDateRandomId()
  5. getRandomDate()