Thursday, December 20, 2007

Java Code For Reading and Writing a Properties File

// Read properties file.
Properties properties = new Properties();
try {
properties.load(new FileInputStream("filename.properties"));
} catch (IOException e) {
}

// Write properties file.
try {
properties.store(new FileOutputStream("filename.properties"), null);
} catch (IOException e) {
}
Here is an example of the contents of a properties file: # a comment
! a comment

a = a string
b = a string with escape sequences \t \n \r \\ \" \' \ (space) \u0123
c = a string with a continuation line \
continuation line
d.e.f = another string

Tuesday, December 18, 2007

Java Code For Getting and Setting Properties

String string = properties.getProperty("a.b");
properties.setProperty("a.b", "new value");

Saturday, November 10, 2007

Java Code For Creating a Set

A set is a collection that holds unique values. Adding a value that's already in the set has no effect. // Create the set

Set set = new HashSet();

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

// Remove elements from the set
set.remove("c");

// Get number of elements in set
int size = set.size(); // 2

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

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

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

// Create an array containing the elements in the set (in this case a String array)
String[] array = (String[])set.toArray(new String[set.size()]);

Thursday, September 6, 2007

Java Code For Comparing Dates

Calendar xmas = new GregorianCalendar(1998, Calendar.DECEMBER, 25);

Calendar newyears = new GregorianCalendar(1999, Calendar.JANUARY, 1);

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


// Get difference in milliseconds
long diffMillis = newyears.getTimeInMillis()-xmas.getTimeInMillis();

// Get difference in seconds
long diffSecs = diffMillis/(1000); // 604800

// Get difference in minutes
long diffMins = diffMillis/(60*1000); // 10080

// Get difference in hours
long diffHours = diffMillis/(60*60*1000); // 168

// Get difference in days
long diffDays = diffMillis/(24*60*60*1000); // 7

Saturday, September 1, 2007

Java Code For Getting the Current Date : java.util.Date

Calendar cal = new GregorianCalendar();

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

Java Code For Creating a Date Object for a Particular Date

Calendar xmas = new GregorianCalendar(1998, Calendar.DECEMBER, 25);
Date date = xmas.getTime();

Java Code to Determine If a Year Is a Leap Year : java.util.Date

GregorianCalendar cal = new GregorianCalendar();
boolean b = cal.isLeapYear(1998); // false
b = cal.isLeapYear(2000); // true
b = cal.isLeapYear(0); // true