Tuesday, February 5, 2013

Java Data Time Format made simple


/**
     * Format a time from a given format to given target format
     * 
     * @param inputFormat
     * @param inputTimeStamp
     * @param outputFormat
     * @return
     * @throws ParseException
     */
    private static String TimeStampConverter(final String inputFormat,
            String inputTimeStamp, final String outputFormat)
            throws ParseException {
        return new SimpleDateFormat(outputFormat).format(new SimpleDateFormat(
                inputFormat).parse(inputTimeStamp));
    }
Sample Usage is as Following:
    try {
        String inputTimeStamp = "Tue Feb 05 13:59:44 IST 2013";

        final String inputFormat = "EEE MMM dd HH:mm:ss z yyyy";
        final String outputFormat = "yyyy.MM.dd GGG hh:mm aaa";

        System.out.println(TimeStampConverter(inputFormat, inputTimeStamp,
                outputFormat));

    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

How and When, does finally block get executed in java - deep thinking from C++ concepts RAI Resource Allocation is Initializtion


Does finally block runs even after block/object/function get returned?

Yes. Even if there was an Exception within catch block, finally will be executed.
If you are familiar with C++, just think finally as the destructor of an object. What ever the state of a statement within the object, ~Destructor will be executed. But you cant put return withinfinally[some compilers allow though].
See the code below: See how global variable y been changed. Also see how Exception1 been covered by Exception2.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace finallyTest
{
    class Program
    {
        static int y = 0;
        static int testFinally()
        {
            int x = 0;
            try
            {
                x = 1;
                throw new Exception("Exception1");
                x = 2;
                return x;
            }
            catch (Exception e)
            {
                x = -1;
                throw new Exception("Exception2", e);
            }
            finally
            {
                x = 3;
                y = 1;
            }
            return x;
        }

        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine(">>>>>" + testFinally());
            }
            catch (Exception e)
            { Console.WriteLine(">>>>>" + e.ToString()); }
            Console.WriteLine(">>>>>" + y);
            Console.ReadLine();
        }
    }
}
output:
    >>>>>System.Exception: Exception2 ---> System.Exception: Exception1
   at finallyTest.Program.testFinally() in \Projects\finallyTest\finallyTest\Program.cs:line 17
   --- End of inner exception stack trace ---
   at finallyTest.Program.testFinally() in \Projects\finallyTest\finallyTest\Program.cs:line 24
   at finallyTest.Program.Main(String[] args) in \Projects\finallyTest\finallyTest\Program.cs:line 38
>>>>>1