java.util(contd..)

Dated: 05-21-2001

 

StringTokenizer

Sample Code

import java.util.StringTokenizer;

class STDemo {

static String in = "Title=Java;" +

" Author= Mr. ABCD;"+

"Publisher=McGraw-Hill";

public static void main (String args[ ]) {

StringTokenizer st = new StringTokenizer (in, "=;");

//each character in the delimiters string is considered a valid delimiter.

while(st.hasMoreTokens()){

String key = st.nextToken();

String value = st.nextToken();

System.out.println (key + "\t" + val);

}

}

}

Output

Title Java

Author Mr. ABCD

Publisher McGraw-Hill

Date

Encapsulates the current date and time. Implements Comparable interface.

Date()

Date(long millis)

 

Method Summary

boolean

after(Date when)
Tests if this date is after the specified date.

boolean

before(Date when)
Tests if this date is before the specified date.

Object

clone()
Return a copy of this object.

int

compareTo(Date anotherDate)
Compares two Dates for ordering.

int

compareTo(Object o)
Compares this Date to another Object.

boolean

equals(Object obj)
Compares two dates for equality.

long

getTime()
Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this
Date object.

int

hashCode()
Returns a hash code value for this object.

void

setTime(long time)
Sets this
Date object to represent a point in time that is time milliseconds after January 1, 1970 00:00:00 GMT.

String

toString()
Converts this
Date object to a String of the form:

 

Sample Code

import java.util.*;

class DateDemo{

public static void main (String args[ ]){

Date date = new Date();

// to current date and time

System.out.println(date);

long msec = date.getTime();

System.out.println("Milliseconds since Jan. 1, 1970 GMT =" + msec);

}

}

Output:

Mon May 21 16:10:40 CST 2001

Milliseconds since Jan. 1, 1970 GMT = 1017192929029

Random

Is a generator of pseudorandom numbers. They are called pseudorandom numbers because they are simply distributed sequences.

Random ()

Random (long seed)

The first version creates a number generator that uses the current tiemas the starting, or seed value. If you use same seed to initialize two Random objects, you will extract the same random sequence.

 

Method Summary

Protected int

next(int bits)
Generates the next pseudorandom number.

Boolean

nextBoolean()
Returns the next pseudorandom, uniformly distributed
boolean value from this random number generator's sequence.

void

nextBytes(byte[] bytes)
Generates random bytes and places them into a user-supplied byte array.

Double

nextDouble()
Returns the next pseudorandom, uniformly distributed
double value between 0.0 and 1.0 from this random number generator's sequence.

Float

nextFloat()
Returns the next pseudorandom, uniformly distributed
float value between 0.0 and 1.0 from this random number generator's sequence.

Double

nextGaussian()
Returns the next pseudorandom, Gaussian ("normally") distributed
double value with mean 0.0 and standard deviation 1.0 from this random number generator's sequence.

int

nextInt()
Returns the next pseudorandom, uniformly distributed
int value from this random number generator's sequence.

int

nextInt(int n)
Returns a pseudorandom, uniformly distributed
int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.

long

nextLong()
Returns the next pseudorandom, uniformly distributed
long value from this random number generator's sequence.

void

setSeed(long seed)
Sets the seed of this random number generator using a single
long seed.

 

Piece of Code

Random r = new Random();

double val;

val = r.nextGaussian();