Friday 25 November 2011

Get geographical location (geolocation) by IP address using jQuery

To get geographical location (geolocation) by IP address it is enough to use the following query:
http://iplocationtools.com/ip_query.php?output=json&ip=80.80.214.93
For more details see this page.

Currency Custom Formatting in Java

To format numbers like currency without currency symbol and without grouping it is enough to declare:
NumberFormat format = new DecimalFormat( "#####0.00" );

If we need to change the locale formatting, then we have to add:
DecimalFormatSymbols dfs = new DecimalFormatSymbols( Local.UK );
NumberFormat format = new DecimalFormat( "#####0.00", dfs );

For more details consult Java API - DecimalFormat.

Wednesday 23 November 2011

Java 7 Adoptation Guide

Java 7 Adoptation Guide is a good place to learn the new features of Java 7.

The most interesting features are:
  1. try-with-resources statement;
  2. catching multiple exceptions;
  3. more precise re-throw of an exception;
  4. diamond syntax for more concise new expressions.

Monday 21 November 2011

Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.
Dr. Seuss

Thursday 17 November 2011

Improve performance by switching from StringBuffer to StringBuilder

If you have a lot of old code and you are pritty sure that you do not use StringBuffer objects shared with different threads, you can upgrade all your project's code, changing StringBuffer with StringBuilder. The advantages could be like 35% of effeciency increment based on this article.

Here you are a couple of Unix scripts for this.

The first script just produces the list of files to be modified.

grep -l -R 'StringBuffer' ./project/src/* > files-to-change.txt

The second script replaces all StringBuffer with StringBuilder, creating .bak file for every modified file.

while read CUR_FILE
do
    echo "$CUR_FILE"
    mv $CUR_FILE $CUR_FILE.bak
    sed 's/StringBuffer/StringBuilder/g' $CUR_FILE.bak > $CUR_FILE
done < files-to-change.txt

If you like chess by correspondence, a good site to be registered on is gameknot.

Wednesday 16 November 2011

Pattern & Matcher in Java

// All values equals to "0,00" but not those 
// which have "0,00" as a part, like "20,00"
final Pattern pattern = Pattern.compile( "\\D*?0,00" );
final Matcher matcher = pattern.matcher( values );
if ( matcher.matches() ) {
      // ... 
}
For more details see this page.

Leading zeros during numbers' formatting in Java

final int number = 1;
// my formatter with leading zeros
final DecimalFormat myFormatter = new DecimalFormat( "00" );
String out = myFormatter.format( number );

For more details see this page.

How to remove the last character in StringBuilder


StringBuilder text;
// ...
final int length = text.length(); 
if ( length > 0 ) {
     // We remove the last character.
     text.deleteCharAt( length - 1 );
}

Thursday 10 November 2011

Wednesday 9 November 2011

Good guide for cron/crontab usage. Some useful commands:

crontab -l      - to list all tasks
crontab -e      - to edit the list using vi
Character consists of what you do on the third and fourth tries. 
James A. Michener