Example: Date conversion

This example converts a date from mm/dd/yy format to month, day, year format. For example, the example converts 6/12/2004 into June 12, 2004. The example assumes that the global variable theDate has been set before the business logic is called. Note how the example uses the following method to obtain the value of the input variable:

IGlobalVariable inputDate = getGlobalVariable(blInfo, "theDate");

After using standard Java™ functions to manipulate the string to represent the date in the desired format, the example uses the following method to put the new string into the same global variable:

setGlobalVariable(blInfo, "theDate", formattedDate);

////////////////////////////////////////////////////////////////////////////////
//  This sample is provided AS IS.
//  Permission to use, copy and modify this software for any purpose and
//  without fee is hereby granted. provided that the name of IBM not be used in
//  advertising or publicity pertaining to distribution of the software without
//  specific written permission.
//
//  IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SAMPLE, INCLUDING ALL
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL IBM
//  BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
//  DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
//  IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
//  OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SAMPLE.
////////////////////////////////////////////////////////////////////////////////
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.ibm.hats.common.IBusinessLogicInformation;
import com.ibm.hats.common.GlobalVariable;
import com.ibm.hats.common.IGlobalVariable;

public class CustomDateFormatter
{
 		public static void execute(IBusinessLogicInformation blInfo)
			{
				IGlobalVariable inputDate = getGlobalVariable(blInfo, "theDate");
				SimpleDateFormat inputFormat = new SimpleDateFormat("MM/dd/yyyy");
				SimpleDateFormat outputFormat = new SimpleDateFormat("MMMM dd, yyyy");
       		
				try 
					{
						Date tempDate = inputFormat.parse(inputDate.getString().trim()); 
						String formattedDate = outputFormat.format(tempDate); 
						setGlobalVariable(blInfo, "theDate", formattedDate);
					} 
						catch (ParseException ex) 
							{
								ex.printStackTrace();
							}
			}
}