Rob Bamforth's Blog
Java, SQL, MySQL, Networking, SQL Server, PHP, COM, DBA + ODBC

Archive for the 'Java' Category

Java – How To Calculate Seconds Since Midnight

October 14, 2009

Here is a simple method to calculate seconds since midnight for the current time.
There are a few methods to achieve the same result. This is a quick and easy way.
public getSecondsSinceMidnight() {         
    DateFormat dateFormat = new SimpleDateFormat();      
    java.util.Date date = new java.util.Date();
    dateFormat = new SimpleDateFormat(“HH”);
    date = new java.util.Date();
    int hour = Integer.parseInt(dateFormat.format(date));         
    [...]

JAVA – How To Calculate An Age From A Date Of Birth (DOB)

June 15, 2009

The following code illustrates how to derive an age from a given DOB.
This example uses the DOB format YYYY-MM-DD.
This can be altered to any format by changing the substrings taken from the original DOB input.
String dob = “1984-09-20″;
//TAKE SUBSTRINGS OF THE DOB SO SPLIT OUT YEAR, MONTH AND DAY
//INTO SEPERATE VARIABLES
int yearDOB = Integer.parseInt(dob.substring(0, 4));
int [...]

Java – Random Number Generator / How To Generate A Random Number

June 9, 2009

This is a simple method for generating a random number between zero (0) and a given number.
The following code example with generate a random number between 0 and 49.
Although the value of maxNumber is set to 50, Java will start the count at 0.
If you want a number between 0 and 50, set the value [...]

JAVA – Set The Position Of The ScrollBar In JScrollPane()

June 4, 2009

The following codes assumes that you have created a JScrollPane() called scrollPane

int x = 0;
int y = 10;
scrollPane.getViewport().setViewPosition(new java.awt.Point(x, y));

In this example x sets the horizontal position of a horizontal scroll bar.
y sets the vertical position of a vertical scroll bar – in this case the vertical scroll bar will appear 10 pixels down from [...]

JAVA – How To Post To A HTML/PHP Post Form

April 27, 2009

This example shows how to build up a string to post to a web form, and return the results generated.
Element1 is the first variable the the post accepts – “Value 1″ is the literal string bieng passed to it.
Element2 is the second variable that the post accepts – “Value 2″ is the literal string bieng [...]

JAVA – Forca a JTextArea (text area) to the screoll to the bottom

April 1, 2009

You can force the JTextArea (text area) to scroll to the bottom by moving the caret to the end of the text area after the call to append:
textarea.append(“Some Text\n”);
textarea.setCaretPosition(textarea.getDocument().getLength()); 

JAVA – Remove Row from A JTable

March 30, 2009

How to remeve a row from a JTable.
This example illustrates how to remove a row from an existing JTable called table.
The row to be deleted will be the row that has been selected by clicking the mouse on it.
 
//CREATE MODEL INSTANCE FROM EXISTING TABLE
DefaultTableModel model = new DefaultTableModel();
model = (DefaultTableModel) table.getModel();
//DELETE THE SELECTED ROW
model.removeRow(table.getSelectedRow());
                
//INSERT A [...]

JAVA Check For A Valid Email Address String

February 12, 2009

How to check for a valid email address string.
Example Code:
String email = “test@test.com”;
Pattern p = Pattern.compile(“.+@.+\\.[a-z]+”);
Matcher m = p.matcher(email);
boolean matchFound = m.matches();
if(matchFound){
System.out.println(“EMAIL OK”);
}else{
System.out.println(“EMAIL ERROR”);
}

Convert A String To Proper Case In Java

February 12, 2009

Converting a string to proper case in Java.
e.g. convert “roBErT” to “Robert”.
Example code:
String forename = “roBErT”;
String forenameforenameInitial = “”;
forename = forename.toLowerCase();
forenameInitial = forename.substring(0, 1).toUpperCase();
forename = forenameInitial + forename.substring(1,forename.length());
System.out.println(forename);
The string vaiable forename now has a value of  “Robert”.

Launch Web Browser From Java Application

February 12, 2009

Opening a web browser from a Java application.
Bare Bones Browser is a great tool for opening a web browser on a given URL from a Java / Swing application.
Step 1 – Firstly create an empty class called BareBonesBrowserLaunch.java.
Copy and paste the entire section of code below into the class:
/////////////////////////////////////////////////////////
//  Bare Bones Browser Launch                          //
//  Version [...]