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, ...
Thursday, June 25, 2009
How to Get the Current Time in Another Time Zone
8:35 PM
Posted by
Techsavy
// 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);
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
8:34 PM
Posted by
Techsavy
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);
// 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
8:34 PM
Posted by
Techsavy
String[] strArray = new String[] {"x", "y", "Z"};
Arrays.sort(strArray);
// [Z, x, y]
Arrays.sort(strArray);
// [Z, x, y]
How to Use Sorted Map
8:33 PM
Posted by
Techsavy
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]
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
8:32 PM
Posted by
Techsavy
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
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
8:31 PM
Posted by
Techsavy
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]
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]
Subscribe to:
Posts (Atom)