Random Class
&
Date Class
1
The Random class
A Random object generates pseudo-random numbers.
Class Random is found in the java.util package.
import java.util.*;
Method name Description
nextInt() returns a random integer
nextInt(max) returns a random integer in the range [0, max)
in other words, 0 to max-1 inclusive
nextDouble() returns a random real number in the range [0.0, 1.0)
Example:
Random rand = new Random();
int randomNumber = rand.nextInt(10); // 0-9
2
Generating random numbers
Common usage: to get a random number from 1 to N
int n = rand.nextInt(20) + 1; // 1-20 inclusive
To get a number in arbitrary range [min, max] inclusive:
name.nextInt(size of range) + min
Where size of range is (max - min + 1)
Example: A random integer between 4 and 10 inclusive:
int n = rand.nextInt(7) + 4;
3
Random questions
Given the following declaration, how would you get:
Random rand = new Random();
A random number between 1 and 47 inclusive?
int random1 = rand.nextInt(47) + 1;
A random number between 23 and 30 inclusive?
int random2 = rand.nextInt(8) + 23;
4
import java.util.Random;
public class JavaRandomExample2 {
public static void main(String[] args) {
Random random = new Random();
//return the next pseudorandom integer value
System.out.println("Random Integer value : "+random.nextInt());
// setting seed
long seed =20;
random.setSeed(seed);
//value after setting seed
System.out.println("Seed value : "+random.nextInt());
//return the next pseudorandom long value
Long val = random.nextLong();
System.out.println("Random Long value : "+val);
}
}
5
import java.util.Random;
public class JavaRandomNextDoubleExample1 {
public static void main(String[] args) {
Random random = new Random();
//return the next pseudorandom double value
Double val = random.nextDouble();
System.out.println("Random Double value : "+val);
Double val1 = random.nextDouble();
System.out.println("Random Double value : "+val1);
}
}
6
Date Class
Date class represents date and time in java. It provides
constructors and methods to deal with date and time in
java.
Constructors
1) Date() Creates a date object representing
current date and time.
2) Date(long Creates a date object for the given
milliseconds) milliseconds since January 1, 1970,
00:00:00 GMT.
7
Date Methods
No. Method Description
1) boolean after(Date tests if current date is
date) after the given date.
2) boolean before(Date tests if current date is
date) before the given date.
3) Object clone() returns the clone object
of current date.
4) int compareTo(Date compares current date
date) with given date.
5) boolean equals(Date compares current date
date) with given date for
equality.
7) long getTime() returns the time
represented by this
date object.
9) void setTime(long time) changes the current 8
Date Methods
✓ getDate()
✓ getHours()
✓ getYear();
✓ getMinutes()
✓ getDay()
✓ getSeconds()
✓ getMonth()
9
After method
import java.util.Date;
public class JavaDateAfterExample1 {
public static void main(String[] args) {
Date d=new Date(2018,9,21);
Date d2=new Date(1997,3,10);
System.out.println("Date 'd' is after Date 'd2' : "+d.after(d2));
}
}
10
before method
import java.util.Date;
public class JavaDateBeforeExample1 {
public static void main(String[] args) {
Date d=new Date(1997,3,10);
Date d2=new Date(2018,9,21);
System.out.println("Date 'd' is before Date 'd2' : "+d.before(d2));
}
}
11
CompareTo method
import java.util.Date;
public class JavaDateCompareToExample1 {
public static void main(String[] args) {
Date d=new Date(1997,3,10);
Date d1=new Date(2018,9,21);
int comparison=d.compareTo(d1);
System.out.println("Your comparison value is : "+comparison);
}
}
12
equals method
import java.util.Date;
public class JavaDateEqualsExample1 {
public static void main(String[] args) {
Date d=new Date(2018,9,21);
Date d1=new Date(1997,3,10);
System.out.println("Date 'd' equals Date 'd1' : "+d.equals(d1));
}
}
13