Interview Questions

Java - Simple Date Format

In this section, Lets discuss about handling Date format in Java application.

1. Lets first start with converting Current time in to a particular Date format (for example "12 Sep 2007 10:46:43:021")

Inorder to achive this functionlity, we are going to use SimpleDateFormat Api provided by Java.

* Create a SimpleDateFormat object using the specified format ( Eg., dd MMM yyyy HH:mm:ss:SSS ) as you wish.

[ Note: For more information about the formatting options, please refer the Java API for Simple Date Format ]

* Get the Current Time Milli Seconds from the System property and create a Date object.
* Using the SimpleDateFormat object, format the Date object which would result in getting the Current time in the format specified by you.

Its so simple!

2. In this part, lets convert the Date in a particular format ( eg., 12 Sep 2007 10:46:43:021 ) to some other format ( eg., 12-09-2007 10:46:43).
* Create a String with value as "12 Sep 2007 10:46:43:021"
* Create a SimpleDateFormat object (say obj1) with the format "dd MMM yyyy HH:mm:ss:SSS"
* Create another SimpleDateFormat object (say obj2) with the format "dd-MM-yyyy HH:mm:ss"
* Using the obj1, parse the input date string in first step which will result in a getting Date object as response.
* Using the obj2, format the Date object which we got in above step. This operation will convert the Date in to our respective format.

Refer the below code for more information.

/**
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package in.techdive.java.examples;

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

public class dateStrToLong
{
    public static void main(String[] args)
    {
        /*
         * Convert Current Time to a particular format
         */

        SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy HH:mm:ss:SSS");
        Date date = new Date(System.currentTimeMillis());
        System.out.println("Current Time = " + sdf.format(date));

        /*
         * Convert Date in some format to Other format
         */

        String inputDateString = "12 Sep 2007 10:46:43:021";
        SimpleDateFormat obj1 = new SimpleDateFormat("dd MMM yyyy HH:mm:ss:SSS");
        SimpleDateFormat obj2 = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");

        try
        {
            System.out.println("\nInitial Format = " + inputDateString);
            Date srcDate = obj1.parse(inputDateString);
            System.out.println("Format after conversion = "
                    + obj2.format(srcDate));
        }
        catch (ParseException e1)
        {
            e1.printStackTrace();
        }
    }
}