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));
[...]
Archive for the 'Java' Category
Java – How To Calculate Seconds Since Midnight
October 14, 2009JAVA – How To Calculate An Age From A Date Of Birth (DOB)
June 15, 2009The 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, 2009This 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 – How To Post To A HTML/PHP Post Form
April 27, 2009This 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, 2009You 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, 2009How 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, 2009How 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, 2009Converting 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”.