Issue with date conversion

Hi,

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.

Cheers,
Akshith

Hi Reamon,

Thanks for you reply!

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…

inDate = “8/9/2011 18:00:00 GMT”
allowNull = “true”
outDate = “8/9/2011 18:00:00 GMT”

Please let me know if there are any flaws in the code…

If not, please let me know a valid way to test the code.

Thanks in Advance!

Regards,
Subhash.

java.util.Date objects do not carry a timezone. They always represent the number of milliseconds since since January 1, 1970, 00:00:00 GMT.

So the key steps in your overall approach are:

  1. When converting a string to a Date object, be sure the string and corresponding format string specify the correct timezone.

  2. When converting a Date object to a string, specify which timezone the data formatter is to use.

Based on this, you’ll need two general purpose services (you probably don’t want to hardcode “MST” into these).

  1. yourPub.services:stringToDate
    [highlight=java]IDataCursor idc = pipeline.getCursor();
    String inString = IDataUtil.getString(idc, “inString”);
    String pattern = IDataUtil.getString(idc, “pattern”);

if( (pattern == null) || (pattern.trim().length() == 0) )
pattern = “yyyy-MM-dd’T’HH:mm:sszzz”; // Use a W3C format as default

if( (inString != null) && (inString.length() > 0) )
{
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(pattern);
java.util.Date d = sdf.parse(inString, new java.text.ParsePosition(0));
IDataUtil.put(idc, “data”, d);
}
idc.destroy();[/highlight]

  1. Use pub.date.formatDate to convert a Date object to a string with the timezone of your choice.

Hi Reamon,

Thanks for you reply!

As you mentioned, I have changed my approach while converting the date from one timezone to other.

Please find the Flow details and Testing results as attachments.

As the inDate “8/10/2011 20:00:00 IST” is converted to inString08/10/2011 08:30:00 MDT”, I guess the code is working as expected.

But since the outDate is still displayed as “8/10/2011 20:00:00 IST” in the developer, i need a valid way to test the service.

I tried saving pipeline to file at the end, but it contains the same values as mentioned above.

So please let me know if there is a valid way to test this.

Thanks In Advance!

Subhash.
Flow Details.doc (39 KB)
Testing Results.doc (88.5 KB)

I think you’ve misunderstood.

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?