I have a requirement to convert any date field of any timezone to corresponding Mountain Timezone. I have written the below java service to meet the above requirement. Seems to be working correctly when I tested using eclipse (IDE), but when I copied the same javaservice and run it from developer ( IS running in Mountain Timezone ) by passing the input I see different output. I also tried to write the output date to a file in the filesystem. But I see the same problem.
Please review the below code and let me know if it meets my requirement and also let me know the valid way to test this in webMethods.
// pipeline in
IDataCursor pipelineCursor = pipeline.getCursor();
Date inDate = (Date)IDataUtil.get( pipelineCursor, "inDate" );
String allowNull = null;
allowNull = IDataUtil.getString( pipelineCursor, "allowNull" );
pipelineCursor.destroy();
if(allowNull == null)
{
allowNull = "true";
}
Date outDate = null;
if (inDate != null)
{
boolean needToConvert = true;
if (allowNull!=null && allowNull.equals("true"))
{
SimpleDateFormat formatter = new SimpleDateFormat("MMddyyyy");
String tempDateS =formatter.format(inDate);
if (tempDateS!=null && tempDateS.equals("11290002"))
{
needToConvert = false;
}
}
if(needToConvert)
{
Calendar Cal = new GregorianCalendar();
TimeZone tz = Cal.getTimeZone();
Cal.setTime(inDate);
long lTime = inDate.getTime()-(Cal.get(Calendar.ZONE_OFFSET) +Cal.get(Calendar.DST_OFFSET));
Calendar mountainCal = new GregorianCalendar(TimeZone.getTimeZone("MST"));
mountainCal.setTimeInMillis(Cal.getTimeInMillis());
long timeMSTLongObj = lTime+(mountainCal.get(Calendar.ZONE_OFFSET)+mountainCal.get(Calendar.DST_OFFSET));
outDate = new Date(timeMSTLongObj);
}
// pipeline out
IDataCursor pipelineCursor_1 = pipeline.getCursor();
IDataUtil.put( pipelineCursor_1, "outDate", outDate);
pipelineCursor_1.destroy();
}
Can you explain the details of how it is not working? What input are you providing and what output are you expecting and getting? Can you describe exactly how the inDate java.util.Date object is constructed?
From what i understand in the posted code,The input of the code you wrote is a date object so your service input should not be a string called “inDate” but an object with the Java wrapper type set to Java.util.date.
When you save this code in WM it will save with no errors but when you run it there will be a class cast exception.
As I mentioned earlier, I need to convert the date object “inDate” (of any timezone) to “outDate” date object in Mountain timezone.
The above java code takes “inDate” date object as input, converts it to Mountain timezone and produces “outDate” date object as output.
When I am running the java service manually from the Developer by providing the input “inDate” = “8/9/2011 20:00:00 IST” and “allowNull” = “true”, below are the results I notice in the results pannel…
You cannot convert a java.util.Date to any particular timezone. It always holds the number of milliseconds since Jan 1, 1970 GMT.
What your service is doing is converting a date object to a string. And then converting that string back to the same date.
The outDate string representation shows IST because the java.util.Date.toString method uses the default timezone of the JVM to convert the date object to a string.
Let me reiterate: java.util.Date does not carry a timezone and cannot be converted to a specific timezone.
If you need the date/time in a particular timezone, convert the date object to a string using pub.date:formatDate.
Having said all that, perhaps its appropriate to back up and ask: what is it that you need to do with the date exactly? Are you sure you need an MST representation?