UNIT II - Chapter 8 - How to utilize the utilities?

Chapter 8          :           How to utilize the utilities?

Java 2 Platform, Micro Edition’s CLDC 1.0 contains some utilities that make the life of the programmer a lot easier. These utilities are very helpful during coding. They save the time and the effort which a programmer has to put during coding. The utilities are mainly concerned with the time, date and collections. CLDC 1.0 also has the support for a random number generator.

There are many changes which you will encounter if you are switching from the Java 2 Platform, Standard Edition. One major difference is that the number of classes is comparatively very few. Also the classes that are there have considerably reduced functionality. Some like the Vector class are based on older versions of Java. Thus we should use these utilities with due consideration to there limited functionality in the ‘Compact, Limited Device Configuration’ 1.0.



This class holds a representation of a specific instant in time, with millisecond precision and has been subset for the Mobile Information Device Profile based on JDK 1.3. The Date class is intended to reflect coordinated universal time (UTC), but it may not do so exactly, depending on the host environment of the Java Virtual Machine. This may be due to the lack of support for ‘leap second’ in most computer clocks.



Calendar is an abstract class for getting and setting dates using integer fields such as YEAR, MONTH and DAY etc. The subclasses of this class interpret a Date according to the rules of a particular calendar system. An object of this class can produce all the time field values needed to implement the date-time formatting for a particular language and calendar style. Calendar can also be used to get the values of many kinds including:

§  Current time







§  AM_PM




Thus we find that the Calendar class is useful for a variety of tasks. Below this paragraph I have given a code snippet which uses both the Date and the Calendar class. In this example the date variable is used to store the information from the instance of DateField or the Calendar. The purpose of the code is to retrieve the day of the week from a date.

Example Snippet: Date & Calendar


// Declare a date variable

Date date ;


// If the value of date field is null get the current time from the Calendar

// else get the date from the date field

if(dField.getDate() == null)


date = Calendar.getInstance().getTime() ;




date = dField.getDate() ;



// Get an instance of the calendar

Calendar calendar =  Calendar.getInstance() ;


// Set the date in the instance of calendar

calendar.setTime(date) ;


//This code sets the value of the ticker in form f1 to the day of the week

// set in the calendar

if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY)


f1.setTicker(new Ticker("Monday")) ;


else if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.TUESDAY)


f1.setTicker(new Ticker("Tuesday")) ;


else if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.WEDNESDAY)


f1.setTicker(new Ticker("Wednesday")) ;


else if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.THURSDAY)


f1.setTicker(new Ticker("Thursday")) ;


else if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY)


f1.setTicker(new Ticker("Friday")) ;


else if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY)


f1.setTicker(new Ticker("Saturday")) ;


else if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)


f1.setTicker(new Ticker("Sunday")) ;





Random number generation is a very useful utility provided by the Compact, Limited Device Configuration 1.0. An instance of the Random class is used to generate a stream of pseudorandom numbers. Random class uses a 48-bit seed, which is then modified using a linear congruential formula. If two instances of this class are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers.


The class Random has 2 types of constructors:

§  public Random()

This constructor constructs a random number generator which is initialized based on the current time of the device.

§  public Random(long seed)

This constructor constructs a random number generator which is initialized based on the single seed given as the parameter.

Example Code: Random Number Generator

I will now show you a method which can generate a random number between the two numbers. The first number is referred to as ‘min’ and should be the smaller of the two inputs. The second number is called ‘max’ and should be the larger of the two. It is assumed that both the numbers are positive. Also the result can be any number between the two inclusive of the larger number. Do remember to construct an instance of the Random class before using this method.

public int getRandom( int min, int max )


    int lVar ;

    lVar = max - min ;

    if(lVar != 0)


      lVar = (this.random.nextInt() / (Integer.MAX_VALUE / lVar)) + 1 ;



    lVar = Math.abs(lVar) ;

    return ( min + lVar ) ;




This class implements an increasable array of objects. Vector contains components that can be accessed using an integer index. The Vector can increase or decrease its size based on the needs of the application. Vector's storage increases in chunks according to the size of its capacityIncrement.


The Vector has 3 kinds of constructors namely:

§  Vector()

This constructor constructs a vector that is empty.

§  Vector(int)

This constructor of class Vector constructs an empty vector with a specified initial capacity.

§  Vector(int, int)

This constructor of class Vector constructs an empty vector with a specified initial capacity and capacity increment.


Best Practice


Vector is a very heavy element. Try to avoid using it as much as possible. Wherever possible try arrays instead. We have researched that by using arrays instead of Vector you can speed up the execution of the program by up to 70% in certain situations!


Note: For saving space, the CLDC implementation is based on JDK 1.1.8, not JDK 1.3.


Example Code Snippet: Vector


//Initializing a vector

Vector v = new Vector ();


// Adding a string to the vector

v.addElement (“Hello”);