You are on page 1of 34

Library Packages

& classes
Java.util package
• This package has several classes like
– Date
– Calendar
– Gregorian Calendar
– Random
– Vector
– Stack
– Enumeration Interface
– Hashtable
2
Date class
• Its represent Current Date &Time.
• Class has two constructors.
S.N. Constructor Description
This constructor allocates a Date object and initializes it so
Date()
1 that it represents the time at which it was allocated,
measured to the nearest millisecond.
This constructor allocates a Date object and initializes it to
Date(long date) represent the specified number of milliseconds since the
2
standard base time known as "the epoch", namely January
1, 1970, 00:00:00 GMT.
Creates a date from a string according to the syntax
3 Date(String)
accepted by parse().

Creates a date
4 Date(int, int, int, int, int)
public Date(int year, int month, int date, int hrs, int min

3
Class constructors
S.N. Method Description

1 boolean after(Date when) This method tests if this date is after the specified date.

2 boolean before(Date when) This method tests if this date is before the specified date.

Object clone()
3 This method return a copy of this object.

int compareTo(Date
4 anotherDate) This method compares two Dates for ordering.

5 boolean equals(Object obj) This method compares two dates for equality.

This method returns the number of milliseconds since January 1,


6 long getTime()
1970, 00:00:00 GMT represented by this Date object.

7 int hashCode() This method returns a hash code value for this object.

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

9 String toString() This method converts this Date object to a String of the form.

4
boolean after(Date when)
import java.util.*;
public class DateDemo {
public static void main(String[] args) {
// create 2 dates object
Date date = new Date(2011, 5, 21);
Date date2 = new Date(2015, 2, 18);
// tests if date 2 is after date and print
boolean after = date2.after(date);
System.out.println("Date 2 is after date: " + after);
// tests if date is after date 2 and print
after = date.after(date2);
System.out.println("Date is after date 2: " + after);
}}
5
int compareTo(Date anotherDate)
 Syntex:- public int compareTo(Date anotherDate)
 Return Value :-
 0 if the argument Date is equal to this Date;
 value less than 0 if this Date is before the Date argument;
 value greater than 0 if this Date is after the Date argument.

import java.util.*;
public class DateDemocomp {
public static void main(String[] args) {
// create two dates
Date d = new Date(98, 5, 21);
Date d1 = new Date(99, 1, 9);

6
// make 3 comparisons with them
int comparison = d.compareTo(d1);
int comparison2 = d1.compareTo(d);
int comparison3 = d.compareTo(d);
// print the results
System.out.println("Comp Result:" + comparison);
System.out.println("Comp2 Result:" + comparison2);
System.out.println("Comp3 Result:" + comparison3);
}
}

7
long getTime()
 Syntex:- -
 Return Value :-
 This method returns how many milliseconds have passed since
January 1, 1970, 00:00:00 GMT

import java.util.*;
public class DateDemo {
public static void main(String[] args) {
Date date = new Date(97, 1, 23); // create a date
long diff = date.getTime(); // print how many seconds
System.out.println(diff + " have passed.");
}
}
8
Gregorian Calendar

Field Explanation

Calendar.YEAR Identifies the year

Calendar.MONTH Identifies the month

Calendar.DAY_OF_MONTH Identifies the day

Calendar.HOUR Identifies the hour

Calendar.MINUTE Identifies the minute

Calendar.SECOND Identifies the second

9
import java.text.*;
import java.util.*;
public class CalendarTest
{
public static void main(String[] args)
{
Calendar cal1 = new GregorianCalendar();
cal1.set(Calendar.MONTH, Calendar.MAY);
System.out.println("Year: " + cal1.get(Calendar.YEAR));
System.out.println("Month: " + (cal1.get(Calendar.MONTH) +
1));
System.out.println("Days: " +
cal1.get(Calendar.DAY_OF_MONTH));
}
}
10
Vactor

There are four constructor in vactor


Vector( )
 Vector(int size)
 Vector(int size, int incr)
 Vector(Collection c)

11
Vactor
S.N. Method & Description
boolean add(E e)
1
This method appends the specified element to the end of this Vector.
void add(int index, E element)
2
This method inserts the specified element at the specified position in this Vector.
boolean addAll(Collection<? extends E> c)
3 This method appends all of the elements in the specified Collection to the end of
this Vector.
boolean addAll(int index, Collection<? extends E> c)
4 This method inserts all of the elements in the specified Collection into this Vector
at the specified position.
void addElement(E obj)
5 This method adds the specified component to the end of this vector, increasing its
size by one.
int capacity()
6
This method returns the current capacity of this vector.
void clear()
7
This method removes all of the elements from this vector.
clone clone()
8
This method returns a clone of this vector.
boolean contains(Object o)
9
This method returns true if this vector contains the specified element.

12
Vactor
S.N. Method & Description
boolean containsAll(Collection<?> c)
10 This method returns true if this Vector contains all of the elements in the
specified Collection.
void copyInto(Object[ ] anArray)
11
This method copies the components of this vector into the specified array.
E elementAt(int index)
12
This method returns the component at the specified index.
Enumeration<E> elements()
13
This method returns an enumeration of the components of this vector.
void ensureCapacity(int minCapacity)
This method increases the capacity of this vector, if necessary, to ensure that it
14
can hold at least the number of components specified by the minimum capacity
argument.
boolean equals(Object o)
15
This method compares the specified Object with this Vector for equality.
E firstElement()
16
This method returns the first component (the item at index 0) of this vector.
E get(int index)
17
This method returns the element at the specified position in this Vector.

13
Vactor
S.N. Method & Description
int hashCode()
18
This method returns the hash code value for this Vector.
int indexOf(Object o)
19 This method returns the index of the first occurrence of the specified element in
this vector, or -1 if this vector does not contain the element.
int indexOf(Object o, int index)
This method returns the index of the first occurrence of the specified element in
20
this vector, searching forwards from index, or returns -1 if the element is not
found.
void insertElementAt(E obj, int index)
21 This method inserts the specified object as a component in this vector at the
specified index.
boolean isEmpty()
22
This method tests if this vector has no components.
E lastElement()
23
This method returns the last component of the vector.
int lastIndexOf(Object o)
24 This method returns the index of the last occurrence of the specified
element in this vector, or -1 if this vector does not contain the element.

14
Vactor
S.N. Method & Description
int lastIndexOf(Object o, int index)
This method returns the index of the last occurrence of the specified element in
25
this vector, searching backwards from index, or returns -1 if the element is not
found.
E remove(int index)
26
This method removes the element at the specified position in this Vector.
boolean remove(Object o)
27 This method removes the first occurrence of the specified element in this Vector
If the Vector does not contain the element, it is unchanged.
boolean removeAll(Collection<?> c)
28 This method removes from this Vector all of its elements that are contained in
the specified Collection.
void removeAllElements()
29
This method removes all components from this vector and sets its size to zero.
boolean removeElement(Object obj)
30
This method removes the first occurrence of the argument from this vector.
void removeElementAt(int index)
31
This method deletes the component at the specified index.

15
Vactor
S.N. Method & Description
protected void removeRange(int fromIndex, int toIndex)
32 This method removes from this List all of the elements whose index is between
fromIndex, inclusive and toIndex, exclusive.
boolean retainAll(Collection<?> c)
33 This method retains only the elements in this Vector that are contained in the
specified Collection.
E set(int index, E element)
34 This method replaces the element at the specified position in this Vector with the
specified element.
void setElementAt(E obj, int index)
35 This method sets the component at the specified index of this vector to be the
specified object.
void setSize(int newSize)
36
This method sets the size of this vector.
int size()
37
This method returns the number of components in this vector.
List <E> subList(int fromIndex, int toIndex)
38 This method returns a view of the portion of this List between fromIndex,
inclusive, and toIndex, exclusive.

16
Vactor
S.N. Method & Description
object[ ] toArray()
39 This method returns an array containing all of the elements in this Vector in the
correct order.
<T> T[ ] toArray(T[ ] a)
40 This method returns an array containing all of the elements in this Vector in the
correct order; the runtime type of the returned array is that of the specified array.
String toString()
41 This method returns a string representation of this Vector, containing the String
representation of each element.
void trimToSize()
42 This method trims the capacity of this vector to be the vector's current
size.

17
import java.util.*;
class VectorDemo{
public static void main(String args[]) {
// initial size is 3, increment is 2
Vector v = new Vector(3,2);
System.out.println("Initial size: " + v.size());
System.out.println("Initial capacity: " +
v.capacity());
v.addElement(new Integer(1));
v.addElement(new Integer(2));
v.addElement(new Integer(3));
v.addElement(new Integer(4));
System.out.println("Capacity after four additions: " +
v.capacity());
v.addElement(new Double(5.45));
18
System.out.println("Current capacity: " + v.capacity());
v.addElement(new Double(6.08));
v.addElement(new Integer(7));
System.out.println("Current capacity: " + v.capacity());
v.addElement(new Float(9.4));
v.addElement(new Integer(10));
System.out.println("Current capacity: " + v.capacity());
System.out.println("First element: " + (Integer)v.firstElement());
System.out.println("Last element: " + (Integer)v.lastElement());

19
// enumerate the elements in the vector.
Enumeration vEnum = v.elements();
System.out.println("\\nElements in vector:");
while(vEnum.hasMoreElements())
System.out.print(vEnum.nextElement() + " ");
System.out.println();
}
}

20
Hashtable class
• Hashtable class creates a data
structure in which you can store data
as key/value paires
• in short hashtable has columns
named key and value.
• It has 4 constructor
– Hashtable()
– Hashtable(int size)
– Hashtable(int size,float ratio)

21
S.N.
Hashtable class Method & Description
void clear()
1
This method clears this hashtable so that it contains no keys.
Object clone()
2
This method creates a shallow copy of this hashtable.
boolean contains(Object value)
3
This method tests if some key maps into the specified value in this hashtable.
boolean containsKey(Object key)
4
This method tests if the specified object is a key in this hashtable.
boolean containsValue(Object value)
5
This method returns true if this hashtable maps one or more keys to this value.
boolean equals(Object o)
8
This method compares the specified Object with this Map for equality, as per the definition in the Map interface.

V get(Object key)
9
This method returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
boolean isEmpty()
11
This method tests if this hashtable maps no keys to values.
Enumeration keys()
12
This method returns an enumeration of the keys in this hashtable.
V put(K key, V value)
14
This method maps the specified key to the specified value in this hashtable.
V remove(Object key)
17
This method removes the key (and its corresponding value) from this hashtable.
int size()
18
This method returns the number of keys in this hashtable.
String toString()
19 This method returns a string representation of this Hashtable object in the form of a set of entries, enclosed in braces and
separated by the ASCII characters ", " (comma and space).

22
Hashtable Example
import java.util.*;
class hashtableEx
{
public static void main(String arg[])
{
Hashtable h1=new Hashtable();
h1.put("fname",“Vipul");
h1.put("lname",“Dholariya");
h1.put("city",“Motimoli");
System.out.println("Name is :"+h1.get("fname"));
System.out.println("LName is :"+h1.get("lname"));
System.out.println("City is :"+h1.get("city"));
System.out.println(h1);

23
Enumeration val=h1.elements();
Enumeration key=h1.keys();
while(key.hasMoreElements())
{
System.out.println(key.nextElement()+“
-->"+val.nextElement());
}
}
}

24
Stack class
• Stack is data structure which follows
LIFO manner to insert or remove
element
• It has only 1 constructor
– Stack()

25
Stack class
S.N. Method & Description

boolean empty()
1
This method tests if this stack is empty.

E peek()
2
This method looks at the object at the top of this stack without removing it from the stack.

E pop()
3
This method removes the object at the top of this stack and returns that object as the value of this function.

E push(E item)
4
This method pushes an item onto the top of this stack.

int search(Object o)
5
This method returns the 1-based position where an object is on this stack.

26
Stack Example
import java.util.*;
class Stackex
{
public static void main(String arg[])
{
Stack s=new Stack();
s.push("bca");
s.push("mca");
System.out.println("search element in stack :->
"+s.search("mca"));
System.out.println("elements are");
for(int i=1;i<3;i++)
{
System.out.println(s.pop()); }
27
Calendar Method
S.N. Method & Description
abstract void add(int field, int amount)
1 This method adds or subtracts the specified amount of time to the given calendar field, based
on the calendar's rules.
boolean after(Object when)
2 This method returns whether this Calendar represents a time after the time represented by the
specified Object.
boolean before(Object when)
3 This method returns whether this Calendar represents a time before the time represented by
the specified Object.
void clear()
4 This method sets all the calendar field values and the time value (millisecond offset from the
Epoch) of this Calendar undefined.
void clear(int field)
5 This method sets the given calendar field value and the time value (millisecond offset from the
Epoch) of this Calendar undefined.
Object clone()
6
This method creates and returns a copy of this object.
int compareTo(Calendar anotherCalendar)
7 This method compares the time values (millisecond offsets from the Epoch) represented by two
Calendar objects.
protected void complete()
8
This method fills in any unset fields in the calendar fields.

28
S.N. Method & Description
protected abstract void computeFields()
9
This method converts the current millisecond time value time to calendar field values in fields[].
protected abstract void computeTime()
10 This method converts the current calendar field values in fields[] to the millisecond time value
time.
boolean equals(Object obj)
11
This method compares this Calendar to the specified Object.
int get(int field)
12
This method returns the value of the given calendar field.
int getActualMaximum(int field)
13 This method returns the maximum value that the specified calendar field could have, given the
time value of this Calendar.
int getActualMinimum(int field)
14 This method returns the minimum value that the specified calendar field could have, given the
time value of this Calendar.
static Locale[] getAvailableLocales()
15 This method returns an array of all locales for which the getInstance methods of this class can
return localized instances.
String getDisplayName(int field, int style, Locale locale)
16 This method returns the string representation of the calendar field value in the given style and
locale.

29
S.N. Method & Description
String getDisplayName(int field, int style, Locale locale)
16 This method returns the string representation of the calendar field value in the given style and
locale.
Map<String,Integer> getDisplayNames(int field, int style, Locale locale)
17 This method returns a Map containing all names of the calendar field in the given style and
locale and their corresponding field values.
int getFirstDayOfWeek()
18 This method gets what the first day of the week is; e.g., SUNDAY in the U.S., MONDAY in
France.
abstract int getGreatestMinimum(int field)
19 This method returns the highest minimum value for the given calendar field of this Calendar
instance.
static Calendar getInstance()
20
This method gets a calendar using the default time zone and locale.
static Calendar getInstance(Locale aLocale)
21
This method gets a calendar using the default time zone and specified locale.
static Calendar getInstance(TimeZone zone)
22
This method gets a calendar using the specified time zone and default locale.
static Calendar getInstance(TimeZone zone, Locale aLocale)
23
This method gets a calendar with the specified time zone and locale.
abstract int getLeastMaximum(int field)
24 This method returns the lowest maximum value for the given calendar field of this Calendar
instance.
30
S.N. Method & Description
abstract int getMaximum(int field)
25
This method returns the maximum value for the given calendar field of this Calendar instance.
int getMinimalDaysInFirstWeek()
This method gets what the minimal days required in the first week of the year are; e.g., if the
26
first week is defined as one that contains the first day of the first month of a year, this method
returns 1.
abstract int getMinimum(int field)
27
This method returns the minimum value for the given calendar field of this Calendar instance.
Date getTime()
28 This method returns a Date object representing this Calendar's time value (millisecond offset
from the Epoch").
long getTimeInMillis()
29
This method returns this Calendar's time value in milliseconds.
TimeZone getTimeZone()
30
This method gets the time zone.
int hashCode()
31
This method Returns a hash code for this calendar.
protected int internalGet(int field)
32
This method returns the value of the given calendar field.
boolean isLenient()
33
This method tells whether date/time interpretation is to be lenient.
31
S.N. Method & Description
boolean isSet(int field)
34 This method determines if the given calendar field has a value set, including cases that the
value has been set by internal fields calculations triggered by a get method call.
abstract void roll(int field, boolean up)
35 This method adds or subtracts (up/down) a single unit of time on the given time field without
changing larger fields.
void roll(int field, int amount)
36 This method adds the specified (signed) amount to the specified calendar field without
changing larger fields.
void set(int field, int value)
37
This method sets the given calendar field to the given value.
void set(int year, int month, int date)
38
This method sets the values for the calendar fields YEAR, MONTH, and DAY_OF_MONTH..
void set(int year, int month, int date, int hourOfDay, int minute)
39 This method sets the values for the calendar fields YEAR, MONTH, DAY_OF_MONTH,
HOUR_OF_DAY, and MINUTE.
void set(int year, int month, int date, int hourOfDay, int minute, int second)
40 This method sets the values for the fields YEAR, MONTH, DAY_OF_MONTH, HOUR, MINUTE,
and SECOND.
void setFirstDayOfWeek(int value)
41 This method sets what the first day of the week is; e.g., SUNDAY in the U.S., MONDAY in
France.
32
S.N. Method & Description
void setLenient(boolean lenient)
42
This method specifies whether or not date/time interpretation is to be lenient.
void setMinimalDaysInFirstWeek(int value)
This method sets what the minimal days required in the first week of the year are; For Example,
43
if the first week is defined as one that contains the first day of the first month of a year, call this
method with value.
void setTime(Date date)
44
This method sets this Calendar's time with the given Date.
void setTimeInMillis(long millis)
45
This method sets this Calendar's current time from the given long value.
void setTimeZone(TimeZone value)
46
This method sets the time zone with the given time zone value.
String toString()
47
This method return a string representation of this calendar.

33
Calendar Example
import java.util.*;
public class CalendarDemo {
public static void main(String[] args) throws
InterruptedException {
Calendar cal = Calendar.getInstance();
System.out.println(" Current time is : " + cal.getTime());
Calendar cal2 = Calendar.getInstance();
Date d = cal2.getTime();
System.out.println(" Next time is : " + d);
}
}

34

You might also like