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

public class DateUtils {

    public static final SimpleDateFormat dateFmt = new SimpleDateFormat("dd/MM/yyyy");

    public String format(Date inputDate) {
        return dateFmt.format(inputDate);


The above code is wrong as its creating a static instance of a SimpleDateFormat. As you aware that the SimpleDateFormat is not thread safe. So the code will ultimately fail if one or more thread access the class at the same time. So we may get different outputs/strange behavior.

So its better create a separate SimpleDateFormat instance for each call.

MAVEN – Reduce build time in local enviornment

In most of the Java projects, Maven is being used as a build tool. We use “mvn –U install” command to build the project. While building the project, the maven will download the dependent jars (mentioned in POM.xml file) from the project specific repository/maven repository. Once the build is completed, then all the downloaded jars will be stored in local machine(C:/Users/<>/.m2).

Please run .m2 command on the Run window and check it out.

This kind of build process will take more time as it needs to download all the jars. But this is not required all the times. We can use the downloaded local jars available in the local repository for building the project in the next time

To do that we need to pass –o option while building our project. For example, mvn –o install. This will speed up our code build time.

toString() using Apache Utility

In my first post, I provided the toString() implementation using Java Reflection. But one main drawback is we should implement the same implementation for all the super classes, otherwise super class fields won’t be printed. So this is an overburden

We do not need to create our own implementation each and every time as Apache is providing a utility class to do this task.

The Util class is ReflectionToStringBuilder from the common-lang.jar. So we should use the toString() method available in the ReflectionToStringBuilder class.

Please refer the below example,

import org.apache.commons.lang.builder.ReflectionToStringBuilder;

public class Model {

    private String field1;

    public String toString() {
        return ReflectionToStringBuilder.toString(this);

But by default the static and transient fields won’t be printed. So use the appropriate method from the ReflectionToStringBuilder. i.e) Use toString(Object object, ToStringStyle style, boolean outputTransients, boolean outputStatics)

An array of 10 Elements with some number are missing, How will you find the missing numbers?

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MissingElementsNoLoop {

    public static void main(String[] args) {

        Integer[] numberArray1 = new Integer[10];
        for (int i = 0; i < numberArray1.length; i++) {
            numberArray1[i] = i + 1;
        Integer[] numberArray2 = {1, 2, 4, 5, 7, 8, 9};
        List list1 = new ArrayList(Arrays.asList(numberArray1));
        System.out.println("All Elements List::" + list1);
        List list2 = new ArrayList(Arrays.asList(numberArray2));
        System.out.println("Input Elements List::" + list2);
        System.out.println("Missed Elements List::" + list1);


Print a string for n number of times without using loop

public class PrintNTimeNoLoop {

     * The main method.
     * @param args the arguments
    public static void main(String[] args) {

        int n = 10;
        String input = "String";
        System.out.println(print(n, input));

     * Prints the character.
     * @param n the n
     * @return the char
    public static String print(int n, String input) {
        if (n == 0)
            return "";
        else {
            return input + "\n"+ print(n - 1, input);



Java program to check if two String are anagram of each other

import java.util.Arrays;

public class Anagram {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(;
        String s1 = br.readLine();
        String s2 = br.readLine();

        if (s1.equals(s2)) {
        } else {
            s1 = s1.replace(" ", "");
            s2 = s2.replace(" ", "");
            char c1[] = s1.toCharArray();
            char c2[] = s2.toCharArray();
            if (new String(c1).equals(new String(c2))) {
                System.out.println("Its an anagram");
            } else {
                System.out.println("Its not an anagram");