Saturday, March 14, 2009

Last Day of the month - Java

Playing with dates is always fun.

Here is one of those....

//Given the month, will return the date of the last day of the month
//parameter is month number (starting from 0)
public static Date getLastDayOfMonth (int month) {

Calendar calendar = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat( format );

Date d = new Date();
d.setMonth( month );

calendar.set(Calendar.MONTH, month);
//System.out.println("Value "+ calendar.get(calendar.MONTH));
int lastDateofMonth = calendar.getActualMaximum(Calendar.DATE);
d.setDate( lastDateofMonth );

return d;
}

Quick Sort - Java Vector

Here is a small code snippet to sort a 2D Vector in Java.

//pos is the column number to be sorted with
private void quickSortVector(Vector v, int start, int end, int pos)
{
int i = start; // index of left-to-right scan
int k = end; // index of right-to-left scan

if (end - start >= 1) // check that there are at least two elements to sort
{
String pivot = (String)((Vector)v.elementAt(start)).get(pos); // set the pivot as the first element in the partition

try {
while (k > i) // while the scan indices from left and right have not met,
{
while (((String)((Vector)v.elementAt(i)).get(pos)).compareToIgnoreCase(pivot) <= 0 && i <= end && k > i) // from the left, look for the first
i++; // element greater than the pivot
while (((String)((Vector)v.elementAt(k)).get(pos)).compareToIgnoreCase(pivot) > 0 && k >= start && k >= i) // from the right, look for the first
k--; // element not greater than the pivot
if (k > i) // if the left seekindex is still smaller than
swapVector(v, i, k); // the right index, swap the corresponding elements
}
swapVector(v, start, k); // after the indices have crossed, swap the last element in
// the left partition with the pivot
quickSortVector(v, start, k - 1, pos); // quicksort the left partition
quickSortVector(v, k + 1, end, pos); // quicksort the right partition
}
catch (ArrayIndexOutOfBoundsException ae) {
System.out.println("Could not sort vector : Array out of bound exception for column "+ pos);
return;
}
catch (Exception e) {
System.out.println("Could not sort vector : Exception "+ e);
return;
}
}
else // if there is only one element in the partition, do not do any sorting
{
return; // the array is sorted, so exit
}
}

Catch JS errors on web page or web applications

Here is a code snippet to catch the script errors which could be generated on your web application.

It should be such that when its development environment, it must show a alert message and when its production environment, it sends a email or logs into a table.

//
// Traps all js errors
function catchJSError(a,b,c,d) {
//var lines= document.body.innerHTML.split('\n');
//var errorline = lines[c];


// Get error line
var source = document.body.innerHTML;
source = source.split("\n");

var bugline = ((c-1) + ":" + source[c-1] + "\n" +
(c) + ":" + source[c] + "\n" +
(c+1) + ":" + source[c+1] + "\n"
);



var bug = "<b>JS Error:</b> " + a +
"<BR><b>Error in the file:</b> " + b +
"<BR><b>On Line:</b> " + c +
"<BR><b>Character:</b> " + (isIE==true)?event.errorCharacter:"0" +
"<BR><b>Window Title:</b> " + document.title +
"<BR><b>Error Type:</b> " + (isIE==true)?e.errorCode:"0" + "<BR><BR>";


var file = window.location.pathname;
file = file.substring (file.lastIndexOf("/"));

var subject = escape("JS error on " + window.location.host + " : " + file);

var live_servers = "prod.checkingserver.com";

// Send email if a live server
if (live_servers.indexOf(document.domain)>=0) {

sendAjaxRequest("reportJSError.do", "subject="+subject+"&bug="+ escape(bug) );
}
else
alert(bug.replace(/<b>/g,"").replace(/<\/B>/g,"").replace(/<BR>/g,"\\n"));


return false;
}
window.onerror=catchJSError;

New blog from me

Hey guys

I have created this new blog to share my small knowledge on software development techniques and the related topics.

I just hope that this helps some of you atleast.