Saturday 26 September 2015

How to Convert String to Date in Java in yyyy-MM-dd format

 In this tutorial, I will show you how to convert String to Date in Java which is in yyyy-mm-dd format. We will use DateFormat and SimpleDateFormat class from java.util.text package for this conversion.

Here are the steps :
1) Create a DateFormat with format String as "yyyy-MM-dd" (remember, small "m" is for minutes, and capital "M" is for month)
2) Call the parse() method with given String, this will return a java.util.Date object

Saturday 19 September 2015

Singleton Design Pattern using Double Checked Locking Idiom

Singleton design pattern using Double checked locking pattern
In my last interview, I was asked to write code to implement Singleton design pattern using "Double checked locking" idiom, which I thought to share with you all. In Singleton design pattern, only instance of class is created during application lifetime and that's shared between all its clients. One of the challenge with Singleton pattern is to ensure that only instance of class is created without compromising performance.

How to convert String to int and Integer in Java? An Example

There are two main way to convert a String to int primitive and Integer class in Java :

1) Integer.parseInt(), which takes a String and return an int primitive

2) Integer.valueOf() ,which accept a String and return an Integer object

Sunday 6 September 2015

Difference between Vector and ArrayList in Java

One of the classic question from from Java interviews is, "what is difference between Vector and ArrayList?" You might have seen this in on your own interviews as well. Though both are implementation of List interface from JDK 1.4 onward, the key difference between them is Vector is not a thread-safe and synchronized Collection but ArrayList is not synchronized. Which means if multiple threads tries to add and remove elements from Vector, it will still be Ok, but ArrayList's structure may be destroyed. If you remember this key factor then you can automatically derive several other differences between Vector and ArrayList las shown below.