Thursday 13 March 2014

Best approach for formatting date in java


The most common approach to format the date as required by using SimpleDateFormat.


This approach causes problems when multiple threads access the same instance of the class variable, due to lack of synchronization.


The below are the most frequent exceptions will occurs:
  • java.lang.NumberFormatException
  • java.lang.ArrayIndexOutOfBoundsException

There are two approaches to handle these exceptions in Multi-threading environment.
  1. By using ThreadLocal<SimpleDateFormat>: ThreadLocal is a best appraoch to implement Per Thread Singleton classes or per thread context information.
  2. FastDateFormat.getInstance(): FastDateFormat is available in commons-lang-version.jar. it has several overloaded methods of getInstance() to provide the date format for Thread-safe environment. Please find the API documents here.

Sample code snippet by using SimpleDateFormat:
package com.samples;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SimpleDateFormatExample {
 
 private static DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
 
 public static void main(String[] args) {
  Date currentDate = new Date();
  System.out.println("Current Time: " + dateFormat.format(currentDate));
 }

}



Sample code snippet by using ThreadLocal:
package com.samples;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ThreadLocalDateFormatExample {

 private static ThreadLocal<DateFormat> dateFormat = new ThreadLocal<DateFormat>() {
  protected DateFormat initialValue() {
   return new SimpleDateFormat("MM/dd/yy");
  }
 };
 
 public static void main(String[] args) {
  Date currentDate = new Date();
  System.out.println("Current Time: " + dateFormat.get().format(currentDate));
 }

}


Sample code snippet by using FastDateFormat:
package com.samples;

import java.util.Date;

import org.apache.commons.lang.time.FastDateFormat;

public class FastDateFormatExample {

 private static final FastDateFormat dateFormat = FastDateFormat.getInstance("dd-MM-yyyy");
 
 public static void main(String[] args) {
  Date currentDate = new Date();
  System.out.println("Current Time: " + dateFormat.format(currentDate));
 }
}

Hence, the best approach is to format the date isby using FastDateFormat if your project has commons-lang.jar dependency, otherwise can use Threadlocal approach.

Wednesday 5 March 2014

Unix basic commands

Below are the few basic Unix commands which will be more useful for Java developers who are working in Unix environment.

List all users:
cat /etc/passwd |grep "/home" |cut -d: -f1
awk -F":" '{ print "username: " $1 "\t\tuid:" $3 }' /etc/passwd

The above commands will list all the users in the system.

Some Basic commands:
cd path - change directory.
mv path1/filename1 path2/filename2 - Move files from one directory to another directory.
ls [options]- list the file names.
ll [options]- list the file names with permissions and size.
cp path/filename1 path2/filename2 - Copy files from one directory to another directory.
cat filename - To print the content of the file.
diff filenmae1 filename2 - To view the differences between two files.
./filename.sh - To run sh files. This is mostly used for executing shell scripts and server stop start.
view filename - To view the content of the file.
vi filename - To view and update the file.
top - To check the CPU performance and utilization.
df - To check the disk space utilized.

vi & view editor commands:
----------------------------
When a opened a file with vi/view command, the file will open in command mode as default. To make changes in the files needs bring to "Insert Mode".
i/<Ins> - insert text before cursor, until <Esc> hit. Also called "Insert Mode".
I - insert text at beginning of current line, until <Esc> hit
<Esc> - Change to "Command Mode".

:w - saves the current file without quitting
:# - move to line #
:$ - move to last line of file
?string - Search backward for string
/string - Search forward for string
n - move to next occurrence of search string
N - move to next occurrence of search string in opposite direction
<Esc>:w! - Saves the file
<Esc>:q! - Quit editing, no save

Create a .jar file:
$ jar -cvf <file_namer.jar> <class_file_path>

To work with above command, first we need to change the directory to JDK installed bin directory and execute the command.

Ex:
$ /opt/WebSphere7/AppServer/java/bin/jar -cvf calculator.jar com/calc/Calculator.class
$ /opt/WebSphere7/AppServer/java/bin/jar -cvf ibank.jar com/calc/*.class

The second command is useful when there are multiple class to be include in a jar. the path /opt/WebSphere7/AppServer/java/bin is a JDK installed path from whepsher server.


Update a .jar file:
jar -uvf <file_namer.jar> <class_file_path>

Ex:
$ /opt/WebSphere7/AppServer/java/bin/jar -uvf calculator.jar com/calc/Calculator.class
$ /opt/WebSphere7/AppServer/java/bin/jar -uvf calculator.jar com/calc/*.class

Unzar a .jar file:
$ jar -xvf <file_namer.jar>

Ex:
$ /opt/WebSphere7/AppServer/java/bin/jar -xvf calculator.jar
Once the above command executed, we will get the class files in unix files system. So first we need to change the directory where we need the files to be located and provide bin full path while executing the command.
If we not change the directory and executed from JDK installed path, all extracted files will be placed in JDK installed path.

Find a Class file in .jar file:
grep classname *.jar

Ex:
grep com/calc/Calculator.class *.jar
grep Ibank.class *.jar

To Know the package exactly:
$ /opt/WebSphere7/AppServer/java/bin/jar -tvf calculator.jar|grep Calculator*

Check HTTP Server status:
$ ps -efw|grep httpd

File system size:
When we are facing with file system space issue and not able to find which directory occupies more memory, use the below command.

du -sm *| sort -nr | head -15
du -sh * | sort -nr | more

To transfer tar files from one IP to another IP:
Login to the IP address, where the file is available and execute the below command.
$ /usr/local/openssh/bin/scp <path>/<file_name>  <userid>@<ip_address>:/tmp/