Home » Archive

Articles Archive for February 2009

Java Codes »

[28 Feb 2009 | No Comment | 190 views]

This example checks if a String is a valid date by parsing the String with an instanceof the SimpleDateFormat class and returns true or false.
import java.text.SimpleDateFormat;import java.text.ParseException;public class DateTest {  public boolean isValidDate(String inDate) {    if (inDate == null)      return false;    //set the format to use as a constructor argument    SimpleDateFormat dateFormat = new SimpleDateFormat(“yyyy-MM-dd”);        if (inDate.trim().length() != dateFormat.toPattern().length())      return false;    dateFormat.setLenient(false);        try {      //parse the inDate parameter      dateFormat.parse(inDate.trim());    }    catch (ParseException pe) {      return false;    }    return true;  }  public static void main(String[] args) {    DateTest test = new DateTest();    System.out.println(test.isValidDate(“2004-02-29″));    System.out.println(test.isValidDate(“2005-02-29″));  }}

The output will be…truefalse…since the year 2004 was a leap year.

Java Codes »

[28 Feb 2009 | No Comment | 127 views]

To run an external program we need a reference to the Runtime which exists as a singleton within every Java runtime.Executing the external program is done by calling the exec() method and it returns a reference to the process that has been created.Now, in this case we run Notepad.exe and there won’t be any output from it written to the standard out, but in other cases there might be and it is possible to catch that information.The Process class has a getInputStream() method that we can use to read the …

Java Codes »

[28 Feb 2009 | No Comment | 163 views]

Getting data from the system clipboard is implemented nicely in Java through the Toolkit class which has a getSystemClipboard method.Since there isn’t necessarily text contents in the clipboard the getSystemClipboard returns an instance of the class Transferable. With that instance we have to test if the content is text before we do any processing with it.To test the code, just copy some text from the browser or some other document and have the java program print it out.
package com.javadb.example;import java.awt.Toolkit;import java.awt.datatransfer.Clipboard;import java.awt.datatransfer.DataFlavor;import java.awt.datatransfer.Transferable;import java.awt.datatransfer.UnsupportedFlavorException;import java.io.IOException;/** * * @author javabout.com */public class Main {    /**     * …

Java Codes »

[28 Feb 2009 | No Comment | 130 views]

It is very easy to place text on the clipboard with Java.We just get an instance of the default toolkit and from that we get hold of a reference to the clipboard object.To place some text on the clipboard we need to pass an object of type Transferable and another object which is the clipboard owner to the method setContents.To make it easy we may specify null as the owner, and as the first argument we pass an instance of the class StringSelection which implements the Transferable interface.To test the …

Java Codes »

[28 Feb 2009 | No Comment | 159 views]

Since strings are immutable, you may want to use the StringBuffer class if you’re going to alter the String in the code. The StringBuffer class can be seen as a mutable String object which allocates more memory (or deallocates if told so) when it’s content is altered.It should be said early that the StringBuffer class i thread-safe which means that it automatically handles synchronization of access to the object by different threads.If you’re using only one thread in your program you should use the StringBuilder class instead since it’s pretty …

Java Codes »

[28 Feb 2009 | No Comment | 110 views]

To find out the minimum and maximum values of a char we can use the static variables MIN_VALUE and MAX_VALUE of the Character class.If we would output the min and max characters themselves it wouldn’t be very readable. Therefore we cast the value to an int to display the decimal values instead.
/* * Main.java * * @author www.javabout.com */public class Main {        /*     * This method displays the minimum and maximum values of the datatype char.     */        public void displayMinAndMaxValuesOfChar() {                System.out.println((int)Character.MIN_VALUE);        System.out.println((int)Character.MAX_VALUE);    }            /**     * @param args the command line arguments     */    public static void main(String[] args) {        new Main().displayMinAndMaxValuesOfChar();    }}

Java Codes »

[28 Feb 2009 | No Comment | 95 views]

The class ‘Integer’ is a wrapper class for the datatype int, and it has two static variables that contains the minimum and maximum values of the int datatype: MIN_VALUE and MAX_VALUE.To find out the min and max values of the int datatype, just print them out to console like this:
/* * Main.java * * @author www.javabout.com */public class Main {        /*     * This method displays the minimum and maximum values of the int datatype.     */        public void displayMinAndMaxValuesOfInt() {                System.out.println(Integer.MIN_VALUE);        System.out.println(Integer.MAX_VALUE);    }            /**     * @param args the command line arguments     */    public static void main(String[] args) {        new Main().displayMinAndMaxValuesOfInt();    }}

The output from the executed code will be:-21474836482147483647

Java Codes »

[28 Feb 2009 | No Comment | 89 views]

The class ‘Doubler’ is a wrapper class for the datatype double, and it has two static variables that contains the minimum and maximum values of the double datatype: MIN_VALUE and MAX_VALUE.To find out the min and max values of the double datatype, just print them out to console like this:
/* * Main.java * * @author www.javabout.com */public class Main {        /*     * This method displays the minimum and maximum values of the datatype double.     */        public void displayMinAndMaxValuesOfDouble() {                System.out.println(Double.MIN_VALUE);        System.out.println(Double.MAX_VALUE);    }            /**     * @param args the command line arguments     */    public static void main(String[] args) {        new Main().displayMinAndMaxValuesOfDouble();    }}

The output from the executed code will be:4.9E-3241.7976931348623157E308

Java Codes »

[28 Feb 2009 | No Comment | 104 views]

Sometimes you want to compute the time a certain operation takes, which can be done by using the currentTimeMillis() method of the System class.The value returned is the current time in milliseconds and by calling the method before and after the operation you can compute the difference in time,which is the time of the operation.The example below performs and operation by looping from 0 to 9 and in each loop makes the thread sleep for 60 milliseconds and then displays the time elapsedbetween the start and the end of this …

Java Codes »

[28 Feb 2009 | No Comment | 141 views]

This code example shows how to display the total amount of memory the Java Virtual Machine uses, the maximum amount of memory the JVM will attempt to use andalso the total amount of free memory in the JVM.It is done through calling the methods totalMemory(), maxMemory() and freeMemory() of the Runtime class.To obtain an instance of the Runtime class we call the static method getRuntime().The amount of memory from the different methods are returned in bytes, so we round the numbers by creating an instance of the DecimalFormat class and …