Utility in Java





Java Utility Package

The java.util package contains the collections framework, legacy collection classes, event model, date and time facilities, internationalization, and miscellaneous utility classes (a string tokenizer, a random-number generator, and a bit array).

java.util.Date class

The java.util.Date class represents a specific instant in time, with millisecond precision.

Constructors:
Date(): 
Creates a date object.
Representing current date-time.

Date(long): 
Creates a date object.
Given mills since Jan 1,1970

Date(String): 
Creates a date object.
For specific date and time.
FORMAT: YYYY/MM/DD hh:mm:ss
Methods:
 int getDate() 
Return current date.

 int getMonth() 
Return current month.

 int getYear() 
Return current year - 1900.

 boolean before(Date when) 
Tests if this date is after the given date.

 boolean after(Date when) 
Tests if this date is before the given date.

 int getHours() 
Return current time hours.

 int getMinutes() 
Return current time minutes.

 int getSeconds() 
Return current time seconds.
Example:
import java.util.Date;
class Test{
public static void main(String[] a)
{
 Date d1, d2, d3;
 d1 = new Date();
 long mil = System.currentTimeMillis();
 d2 = new Date(mil);
 d3 = new Date("2011/3/23 12:00:10");
 System.out.println(d1);
 System.out.println(d2);
 System.out.println(d3);
 }
}
OUTPUT :
Mon Apr 17 12:31:09 IST 2017
Mon Apr 17 12:31:09 IST 2017
Wed Mar 23 12:00:10 IST 2011

java.util.Random class

The java.util.Random class instance is used to generate a stream of pseudorandom numbers. Following are the important points about Random:
  • The class uses a 48-bit seed, which is modified using a linear congruential formula.
  • The algorithms implemented by class Random use a protected utility method that on each invocation can supply up to 32 pseudorandomly generated bits
  • .
Constructors:
Random(): 
Creates a Random object.

Random(long): 
This creates a new random number.
Here generator use a single long seed.
Methods:
 int  nextInt() 
 long nextLong() 
This method returns the next pseudorandom uniformly distributed int value from this random number generator sequence.

 int nextInt(int)
This method returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator sequence.

 float  nextFloat() 
 double nextDouble() 
This method returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator sequence.

 void setSeed(long) 
This method sets the seed of this random number generator using a single long seed.
Example:
import java.util.Random;
class Test{
 public static void main(String[] args) 
 {
  Random ob = new Random();
  int r1 = ob.nextInt(100);
  int r2 = ob.nextInt(100);
  int r3 = ob.nextInt(100);
  System.out.println("RANDOM NUMBERS:");
  System.out.println("From 0 to 100");
  System.out.println(r1);
  System.out.println(r2);
  System.out.println(r3);
 }
}
OUTPUT :
RANDOM NUMBERS:
From 0 to 100 :
41
42
15

java.util.Arrays class

The java.util.Arrays class contains a static factory that allows arrays to be viewed as lists.Following are the important points about Arrays:
  • This class contains various methods for manipulating arrays (such as sorting and searching).
  • The methods in this class throw a NullPointerException if the specified array reference is null.
  • .
Methods:

 static List asList(Object... a)
This method returns a fixed-size list backed by the specified array.

 static int binarySearch(XXX[] a,XXX key)
This method searches the specified array of any type for the specified value using the binary search algorithm.
Here XXX may be any primitive data-type.

 static void fill(XXX[] a, XXX val)
This method assigns the specified type value to each element of the specified array of booleans.
Here XXX may be any primitive data-type.

 static void sort(XXX[] a)
This method sorts the specified array of any type into ascending numerical order.
Example:
import java.util.*;
class Test{
 public static void main(String[] args) 
 {
  int[] ar = {33,44,11,22};   
  print(ar);
  System.out.println("\n----------");
  Arrays.sort(ar);
  print(ar);        
 }
 static void print(int[] ar){
  for(int x: ar){
   System.out.print(x+" ");
  }
 }
}
OUTPUT :
33 44 11 22 
-----------
11 22 33 44

java.util.Collections class

The java.util.Collections class consists exclusively of static methods that operate on or return collections.Following are the important points about Collections:
  • It contains polymorphic algorithms that operate on collections, "wrappers", which return a new collection backed by a specified collection.
  • The methods of this class all throw a NullPointerException if the collections or class objects provided to them are null.
  • .
Methods:

 static void sort(list)
This method sort the elements of the specified list.

 static void reverse(list)
This method reverses the order of the elements in the specified list.

 static void rotate(list, int distance)
This method rotates the elements in the specified list by the specified distance.

 static int frequency(list, Object o)
This method returns the number of elements in the specified collection equal to the specified object.

 static void fill(list, Object o)
This method replaces all of the elements of the specified list with the specified element.

 static void swap(list, int i, int j)
This method swaps the elements at the specified positions in the specified list.

 static void shuffle(list)
This method randomly permutes the specified list using a default source of randomness.

Example:
import java.util.*;
class Test{
 public static void main(String[] args) 
 {
  ArrayList al;
  al = new ArrayList<>();
  al.add(11);
  al.add(32);
  al.add(28);
  al.add(10);
  
  System.out.println(al);
  Collections.sort(al);
  System.out.println(al);
  Collections.reverse(al);
  System.out.println(al);
  Collections.shuffle(al);
  System.out.println(al);
 }
}
OUTPUT :
[11, 32, 28, 10]
[10, 11, 28, 32]
[32, 28, 11, 10]
[10, 32, 28, 11]

Next topic is java-features-from-1-to-4




 










Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in



CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved