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

public class DateTest {

	public static void main(String[] args) {
		try {
            TimeZone gmtZero = TimeZone.getTimeZone("UTC"); // same as GMT0
            TimeZone gmtPlus10 = TimeZone.getTimeZone("GMT+10");
            TimeZone gmtPlus12 = TimeZone.getTimeZone("GMT+12");
            TimeZone gmtMinusFive = TimeZone.getTimeZone("GMT-5");
            SimpleDateFormat gmtPlus10InputFormat = new SimpleDateFormat("HH:mm dd/MM/yyyy"); // I don't specify time zone in the format so your user's don't have to enter the timezone for input
            gmtPlus10InputFormat.setTimeZone(gmtPlus10);
            SimpleDateFormat gmtPlus10Format = new SimpleDateFormat("HH:mm dd/MM/yyyy z");
            gmtPlus10Format.setTimeZone(gmtPlus10);
            SimpleDateFormat gmtPlus12Format = new SimpleDateFormat("HH:mm dd/MM/yyyy z");
            gmtPlus12Format.setTimeZone(gmtPlus12);
            SimpleDateFormat gmtZeroFormat = new SimpleDateFormat("HH:mm dd/MM/yyyy z");
            gmtZeroFormat.setTimeZone(gmtZero);
            SimpleDateFormat gmtMinus5Format = new SimpleDateFormat("HH:mm dd/MM/yyyy z");
            gmtMinus5Format.setTimeZone(gmtMinusFive);

			/* user inputs date time */
            long userTime = gmtPlus10InputFormat.parse("14:30 21/12/2008").getTime(); // user enters time in GMT+10 (can use any time zone)
            System.out.println("userTime millis in GMT+10:                      " + userTime + " - " + gmtPlus10Format.format( new Date(userTime) ));

            /* convert user time to universal time */

            // adjust userTime to univeralTime; store this value in the database
			// Converting the time to universal time essentially means we store the date, time, and time zone in one field
			// the time zone is required so we can convert the date time to any time zone for display and/or input
			long universalTime = userTime + gmtPlus10.getOffset( userTime       );
            System.out.println("userTime millis in universal time (GMT0):       " + universalTime + " - " + gmtZeroFormat.format( new Date(universalTime) ));

            /*
            	convert universalTime to user time

            	Use the converted value for display to the user in their time zone
            */

            long userTimeInPlus12 = universalTime - gmtPlus12.getOffset( universalTime ); // adjust universalTime to user time (GMT+12)
            System.out.println("userTime millis in GMT+12:                      " + userTimeInPlus12 + " - " + gmtPlus12Format.format( new Date(userTimeInPlus12) ));

            long userTimeInPlus10FromUniversalTime = universalTime - gmtPlus10.getOffset( universalTime ); // adjust universalTime to user time (GMT+10)
            System.out.println("userTime millis from universalTime in GMT+10:   " + userTimeInPlus10FromUniversalTime + " - " + gmtPlus10Format.format( new Date(userTimeInPlus10FromUniversalTime) ));

			long userTimeInMinus5 = universalTime - gmtMinusFive.getOffset( universalTime ); // adjust universalTime to user time (GMT+12)
            System.out.println("userTime millis in GMT-5:                       " + userTimeInMinus5 + " - " + gmtMinus5Format.format( new Date(userTimeInMinus5) ));
        }
        catch (Exception e) {
            e.printStackTrace();
        }
	}
}
