Thursday, June 25, 2009

How to Compare Dates


Calendar cal1 = new GregorianCalendar(2007, Calendar.SEPTEMBER, 1);
Calendar cal2 = new GregorianCalendar(2007, Calendar.SEPTEMBER, 15);

// Determine which is earlier
boolean b = cal1.after(cal2); // false
b = cal1.before(cal2); // true

// Get difference in milliseconds
long diffMillis = cal2.getTimeInMillis()-cal1.getTimeInMillis(); //1209600000

How to Create a Date Object


Calendar cal = new GregorianCalendar(2007, Calendar.SEPTEMBER, 1);
Date date = cal.getTime(); // Sat Sep 01 00:00:00 PDT 2007

How to Get the Current Date


Calendar cal = new GregorianCalendar();

// Get the components of the date
int era = cal.get(Calendar.ERA); // 1
int year = cal.get(Calendar.YEAR); // 2009
int month = cal.get(Calendar.MONTH); // 0=Jan, 1=Feb, ...
int day = cal.get(Calendar.DAY_OF_MONTH); // 25
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK); // 1=Sunday, 2=Monday, ...

How to Get the Current Time in Another Time Zone


// Get the current time in London
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("London"));

int hour12 = cal.get(Calendar.HOUR);
int minutes = cal.get(Calendar.MINUTE);
int seconds = cal.get(Calendar.SECOND);
int am = cal.get(Calendar.AM_PM);

// Get the current hour-of-day at GMT
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
int hour24 = cal.get(Calendar.HOUR_OF_DAY);

How to Get the Current Time


Calendar cal = new GregorianCalendar();

// Get the components of the time
int hour12 = cal.get(Calendar.HOUR);
int hour24 = cal.get(Calendar.HOUR_OF_DAY);
int minute = cal.get(Calendar.MINUTE);
int second = cal.get(Calendar.SECOND);
int millisecond = cal.get(Calendar.MILLISECOND);
int ampm = cal.get(Calendar.AM_PM);

How to Sort an Array


String[] strArray = new String[] {"x", "y", "Z"};
Arrays.sort(strArray);
// [Z, x, y]

How to Use Sorted Map


Keys in the TreeMap will automatically be sorted.

Map map = new TreeMap();
map.put("a", new Integer(1));
map.put("c", new Integer(3));
map.put("b", new Integer(2));
// [a=1, b=2, c=3]

How to Use Hash Table


Difference between HashMap and Hashtable :

1) HashMap accepts null key, whereas Hashtable doesn't.
2) HashMap is not thread-safe, whereas Hashtable is thread-safe.

Map map = new Hashtable();
map.put("a", new Integer(1));
map.put("b", new Integer(2));
map.put("c", new Integer(3));
//map.put(null, new Integer(1)); // not allowed

How to Retain the Insertion Order in a Map


Use LinkedHashMap to retain

Map map = new LinkedHashMap();
// Add some elements
map.put("a", new Integer(1));
map.put("b", new Integer(2));
map.put("c", new Integer(3));

// List the entries
for (Iterator it=map.keySet().iterator(); it.hasNext(); ) {
Object key = it.next();
Object value = map.get(key);
}
// [a=1, b=2, c=3]

How to Iterate the Map


// Iterate over the keys in the map
Iterator it = map.keySet().iterator();
while (it.hasNext()) {
// Get key
Object key = it.next();
}

// Iterate over the values in the map
it = map.values().iterator();
while (it.hasNext()) {
// Get value
Object value = it.next();
}

How to Remove the Entry from the Map


// Create a hash map
Map map = new HashMap();

// Add key/value pairs to the map
map.put("a", new Integer(1));
map.put("b", new Integer(2));
map.put("c", new Integer(3));

// remove method returns the value of removed key
oldValue = map.remove("c"); // 3

How to Create a Map


Map is a list of key-value pairs

// Create a hash map
Map map = new HashMap();

// Add key/value pairs to the map
map.put("a", new Integer(1));
map.put("b", new Integer(2));
map.put("c", new Integer(3));

How to Use Sorted Set


Elements in the TreeSet will automatically be arranged in sorted form.

// Create the sorted set
Set set = new TreeSet();

// Add elements to the set
set.add("b");
set.add("c");
set.add("a");

//[a,b,C]

How to Retain the Insertion Order in a Set


Use LinkedHashSet to retain the insertion order

Set set = new LinkedHashSet();

// Add some elements
set.add("1");
set.add("2");
set.add("3");
set.add("1");

// Iterate the set
for (Iterator it=set.iterator(); it.hasNext(); ) {
Object o = it.next();
}
// [1, 2, 3]

How to Add and Remove One Set to Another


// Create the sets
Set set1 = new HashSet();
Set set2 = new HashSet();

set1.add("A");
set2.add("A");
set2.add("B");

// set1=set1+set2-common elements
set1.addAll(set2); // set1=["A","B"]

// set1=set1-set2
set1.removeAll(set2); // set1=[]

How to Iterate a Set


Iterator it = set.iterator();
while (it.hasNext()) {
// Get the element
Object element = it.next();
}

How to Create a Set


Difference between List and Set : List can contain duplicate elements. Set can contain only unique elements. If you try to add the same element again then it will overwrite the new element on old one.

// Create the set
Set set = new HashSet();
// Add elements to the set
set.add("X");
set.add("Y");
set.add("Z");

// Adding an element that already exists in the set has no effect
set.add("X");
size = set.size(); // 3

// Remove elements from the set
set.remove("Z"); //set=["X","Y"]


// Determining if an element is in the set
boolean b = set.contains("A"); // true
b = set.contains("Z"); // false

How to Add and Remove One List from Another


// Create the lists
List samplelist1 = new ArrayList();
List samplelist2 = new ArrayList();

samplelist1.add("A");
samplelist2.add("B");

// Copy all the elements from samplelist2 to samplelist1 (samplelist1=samplelist1+samplelist2)
samplelist1.addAll(samplelist2); // samplelist1=["A","B"]

// Remove all the elements in samplelist1 from samplelist2 (samplelist1=samplelist1-samplelist2)
samplelist1.removeAll(samplelist2); // samplelist1=["A"]

How to Sort a List


// Create a list
String[] strArray = new String[] {"e", "a", "A"};
List list = Arrays.asList(strArray);

// Sort
Collections.sort(list); // A, a, e
 

Sample Java Codes Copyright © 2008 Green Scrapbook Diary Designed by SimplyWP | Made free by Scrapbooking Software | Bloggerized by Ipiet Notez