Java Code Snippits

From Kb

Jump to: navigation, search

Contact Article Author | Blog of Article Author | FirstPartners.net Home | LinkedIn profile of Author

Contents

Java Code Samples

Testing for Remainder

int remainder = 11%2  //This should give a value of 1

Java 5 For Each Loop

Sample for the Sun Java site

void cancelAll(Collection<TimerTask> c) {
    for (TimerTask t : c)
        t.cancel();
}

Reflection - calling all methods on a class

for (Method method : getClass().getMethods())
          {
               if (method.getName().startsWith("get"))
               {
                    try
                    {
                         start(method.getName());
                         method.invoke(this, new Object[0]);
                    }
                    catch (Throwable t)
                    {
                         t.printStackTrace();
                    }
 
               }
          }

Formatting a Date

Date now = new Date(System.currentTimeMillis());
SimpleDateFormat df = new SimpleDateFormat();
String s = df.format(now);

Loading Xml Resource from the Classpath

Personal tools