开发者

Axis web service datetime to java calendar

I've noticed that a wsdl datetime, when using java and axis gets converted to a Calendar object with local machine timezone information. I am in a situation where I need to know the GMT offset that is passed over in the datetime field, but it seems it is using some kind of DateFormat to parse which becomes a Date which normalizes it to GMT开发者_如何学运维 time, and is then converting it to a Calendar object with the local machine timezone. Is there a simple way of getting a Calendar object set to the TimeZone GMT offset that is specified in the WSDL XML field sent over? Or is there a way I can access that string field myself to do my own parsing?

Thanks, BBB


I'm not shure if this is correct. My case is that I get a datetime from a webservice and Axis transforms it to a Calendar with 1h more than the value received from the webservice.

e.g.:

   2015-11-19T12:00:00 from webservice

   2015-11-19T13:00:00 got from parsed Axis object

c.get(Calendar.ZONE_OFFSET) = 3600000

You can create a Date with the correct date time simply:

Date correctDate = new Date(c.getTime().getTime() - c.get(Calendar.ZONE_OFFSET));


I've hit similar issues but it has always been because I've been using getTime() on the returned Calendar object, then using a date formatter on it. The issue is in the getTime() method.

If you interrogate the Calendar object directly you can get the information exactly as the webservice sends it on.

Specifically around the timezone, assuming the Calendar object is c.

c.get(Calendar.ZONE_OFFSET)

will get you the timezone offset in milliseconds. You can then work with that value as you wish

Below is the code I used for my case, where I needed to get it back into an xsd:datetime formatted string. There is no doubt a better way to write it so if anyone reads this and can refine the code I would be grateful.

public String calendarToXsdDateTimeString(Calendar c){
        StringBuffer outputStringBuffer = new StringBuffer();
        //2011-08-22T11:21:57
        outputStringBuffer.append(c.get(Calendar.YEAR));
        outputStringBuffer.append("-");
        outputStringBuffer.append(prefixZeroIfRequired(c.get(Calendar.MONTH)+1));
        outputStringBuffer.append("-");
        outputStringBuffer.append(prefixZeroIfRequired(c.get(Calendar.DAY_OF_MONTH)));
        outputStringBuffer.append("T");
        outputStringBuffer.append(prefixZeroIfRequired(c.get(Calendar.HOUR_OF_DAY)));
        outputStringBuffer.append(":");
        outputStringBuffer.append(prefixZeroIfRequired(c.get(Calendar.MINUTE)));
        outputStringBuffer.append(":");
        outputStringBuffer.append(prefixZeroIfRequired(c.get(Calendar.SECOND)));
        outputStringBuffer.append(getTimeZoneFromCalendar(c));
        return outputStringBuffer.toString();
    }

    public String prefixZeroIfRequired(int value){
        if(value<10 && value >=0){
            return "0"+value;
        }
        else if(value <0 && value > -10){
            String s = value+"";
            String s1 = s.charAt(0)+"0"+s.charAt(1);
            return s1;
        }
        else{
            return ""+value;
        }

    }

    private String getTimeZoneFromCalendar(Calendar c){
        StringBuffer outputStringBuffer = new StringBuffer();
        if(c.get(Calendar.ZONE_OFFSET) >= 0){
            outputStringBuffer.append("+");
        }
        outputStringBuffer.append(prefixZeroIfRequired(c.get(Calendar.ZONE_OFFSET)/1000/60/60));
        outputStringBuffer.append(":"+Math.abs(c.get(Calendar.ZONE_OFFSET)/1000/60)%60);
        return outputStringBuffer.toString();
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜