sexta-feira, 23 de outubro de 2009

Java: Date formatting in java using SimpleDateFormat

Date formatting in java:

public static Calendar parseTimestamp(String timestamp,Locale locale)
throws Exception {

SimpleDateFormat sdf = new SimpleDateFormat
("dd-MMM-yyyy HH:mm:ss", locale);
Date d = sdf.parse(timestamp);

Calendar cal = Calendar.getInstance();
cal.setTime(d);
return cal;

}


public String getStrDate(Date date,String format, Locale locale) {
SimpleDateFormat formatter;
String str = "";

formatter = new SimpleDateFormat(format, locale);
if (this.getDate() != null) {
str = formatter.format(date.getTime());
}
return str;
}

quarta-feira, 3 de junho de 2009

Java: System.properties

Different Operating Systems usually require a different syntax. A good example is the file separator char, the line separator or the path separator.

In order not to hard code these values on your source code there's the System.getProperty(String str) static method that receives a String and returns the desired property.

Take a look at http://java.sun.com/docs/books/tutorial/essential/environment/sysprop.html for further information.

Java: get Locale Date String

After struggling with the GregorianCalendar, Date and DateFormat classes for a while, here's a simple wrapper to replace the now deprecated date.getLocalizedString():


  public static String getLocaleDate(Date date, Locale locale) {

    return SimpleDateFormat.getDateInstance(
        SimpleDateFormat.SHORT,locale).format(date).toUpperCase();
  }

terça-feira, 12 de maio de 2009

Java: Call method by reflection

Here's a simple and yet handy function that one can easily include in an Utility class that given an object and one of its method's name, invokes it (with no parameters).


public void callIt(String funcName, Object obj)
{
try
{
Class class= obj.getClass();
Method funcToCall = _class.getMethod(funcName, null);
funcToCall.invoke(obj, null);
}
catch ( Exception _e )
{
System.out.println("Failed to invoke function " + _e);
}
}

quarta-feira, 7 de janeiro de 2009

.NET LDAP integration COMException (0x80005000): Unknown error (0x80005000)

Be carefull when working with DirectoryServices in .NET
The following exception:

Exception info:
[COMException (0x80005000): Unknown error (0x80005000)]

can be caused by specifying the LDAP Domain in lowercase when creating the DirectoryEntry:

wrong:
DirectoryEntry entry = new DirectoryEntry("ldap://ldap.myserver.com","domain\\user","pass");
correct:
DirectoryEntry entry = new DirectoryEntry("LDAP://ldap.myserver.com","domain\\user","pass");

terça-feira, 6 de janeiro de 2009

Tomcat: wrong version of the project

It may happen sometimes when we need to have several branches/versions of one project, that when we deploy one to tomcat another one is actually referenced. In that case even though we deploy one version, in fact we get a reference to another version.
To check if this is the case we can go to $tomcat/conf/Catalina/localhost/.xml and see if the project name references another version. One simple solution to this problem is to rename/remove the mentioned file.

segunda-feira, 5 de janeiro de 2009

Javascript: return from an iframe


Sometimes when we use iframes (I know we all should avoid them and I most certainly do so, but when you can't really avoid them) and we need to from an inner page navigate to another page but in the main window it may not be straightforward. The problem is you never know exactly if window.parent is the root window, since it might be another iframe. In order to overcome this pitfall I implemented a simple javascript function to solve this problem:

function getRootWin(){
var win = window;
while(win.parent != null && win != win.parent){
win = win.parent;
}
return win.parent;
}

This function gets the root window so we can now do something like

win = getRootWin();
win.location.href= url;

and so go to the target page, getting rid of all the iframes the page be inside of.

Find which jar contains a class


http://www.findjar.com/ is a very useful site in which one can enter a class name and the site will return the jars which include the given class file.

Eclipse: the import xxx cannot be resolved


It has often happened to me that Eclipse didn't find the correct reference to a class despite the fact the jar in which the class was included was already in the project classpath.

After googling for a while I found this discussion which in fact solved my problems:

http://www.brunningonline.net/simon/blog/archives/001748.html

To sum it up you can either:
- Clean the project
- Switch Workspace
- Remove all src folders except the root one.

However it is strongly recomended to read the reference above for complete understanding of the steps to be done.

sexta-feira, 2 de janeiro de 2009

javascript: get URL parameter

To get a URL parameter using javascript, we can use the following function:

function gup( name )
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}

As explained here in netlobo.com